Не отображается изображение ImageView Android
При запуске кода, не отображается изображение (просто белый экран). Все логкаты пишут что файл существует, и разрешения все есть, в чём секрет? Пробовал и через стандартные методы и сейчас пробую через Glide, но результата нету.
P.S. Изображение находится на SD карте
package com.example.mvpphoto;
import androidx.appcompat.app.AppCompatActivity;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import java.io.File;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
File fileimg = new File("sdcard/IMG_20200711_100133.jpg");
Uri uri = Uri.parse(fileimg.getAbsolutePath());
Log.d("Image", fileimg.getAbsolutePath());
Log.d("Image_uri", uri.toString());
Boolean bik = fileimg.exists();
Log.d("Image", bik.toString());
Glide.with(this).load(uri).into(imageView);
}
}
И код манифеста
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mvpphoto">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Ответы (1 шт):
Автор решения: Sergei Buvaka
→ Ссылка
Если вы хотите получить данные из локального хранилища пользователя, вам не достаточно просто прописать пермишены в Manifest. Вы должны явно запросить пермишены у пользователя:
Этот кусок кода показывает как можно обработать ответ пользователя:
// Register the permissions callback, which handles the user's response to the
// system permissions dialog. Save the return value, an instance of
// ActivityResultLauncher, as an instance variable.
private ActivityResultLauncher<String> requestPermissionLauncher =
registerForActivityResult(new RequestPermission(), isGranted -> {
if (isGranted) {
// Permission is granted. Continue the action or workflow in your
// app.
} else {
// Explain to the user that the feature is unavailable because the
// features requires a permission that the user has denied. At the
// same time, respect the user's decision. Don't link to system
// settings in an effort to convince the user to change their
// decision.
}
});
Этот кусок кода показывает как можно запросить разрешение у пользователя:
if (ContextCompat.checkSelfPermission(
CONTEXT, Manifest.permission.REQUESTED_PERMISSION) ==
PackageManager.PERMISSION_GRANTED) {
// You can use the API that requires the permission.
performAction(...);
} else if (shouldShowRequestPermissionRationale(...)) {
// In an educational UI, explain to the user why your app requires this
// permission for a specific feature to behave as expected. In this UI,
// include a "cancel" or "no thanks" button that allows the user to
// continue using your app without granting the permission.
showInContextUI(...);
} else {
// You can directly ask for the permission.
// The registered ActivityResultCallback gets the result of this request.
requestPermissionLauncher.launch(
Manifest.permission.REQUESTED_PERMISSION);
}
Более подробно можно прочесть в документации.