我必须将图像从 sd 卡放置到 android 中的 imageview
我尝试使用 getExternalStorageDirectory()
。它显示已弃用的方法。它不起作用。
主要 Activity
public class MainActivity extends Activity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.imageView1);
try {
Bitmap picture = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath()+"/DCIM/mm.png");
imageView.setImageBitmap(picture);
} catch (Exception e) {
Log.e("Error reading file", e.toString());
}
// File imgFile = new File("/sdcard/DCIM/mm.png");
//
// if(imgFile.exists()) {
//
// Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
//
// imageView.setImageBitmap(myBitmap);
// }
}
}
xml文件
<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".MainActivity"
android:orientation = "vertical">
<ImageView
android:id = "@+id/imageView1"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
/>
</LinearLayout>
我尝试了很多方法。但它不起作用。请建议我这样做。
最佳答案
list 添加此权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
在您的 Activity 中声明:
String[] PERMISSIONS = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE,android.Manifest.permission.READ_EXTERNAL_STORAGE};
private Context mContext=MainActivity.this;
private static final int REQUEST = 112;
static ImageView muyimage;
在 Oncreate() 中;
muyimage=(ImageView)findViewById(R.id.myimge);
if (Build.VERSION.SDK_INT >= 23) {
String[] PERMISSIONS = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (!hasPermissions(mContext, PERMISSIONS)) {
ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST );
} else {
try {
//here i have mentioned my image path ,you have to set your image path
Bitmap picture = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath()+"/Pictures/Screenshots/mine.png");
muyimage.setImageBitmap(picture);
} catch (Exception e) {
Log.e("Error reading file", e.toString());
}
}
} else {
try {
//here i have mentioned my image path ,you have to set your image path
Bitmap picture = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath()+"/Pictures/Screenshots/mine.png");
muyimage.setImageBitmap(picture);
} catch (Exception e) {
Log.e("Error reading file", e.toString());
}
}
还有这个
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//do here
} else {
Toast.makeText(mContext, "The app was not allowed to write in your storage", Toast.LENGTH_LONG).show();
}
}
}
}
private static boolean hasPermissions(Context context, String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
try {
//here i have mentioned my image path ,you have to set your image path
Bitmap picture = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath()+"/Pictures/Screenshots/mine.png");
muyimage.setImageBitmap(picture);
} catch (Exception e) {
Log.e("Error reading file", e.toString());
}
return false;
}
}
}
return true;
}
关于android - 将SD卡中的图像放入android中的imageview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58515287/