如何在从内部存储器选择的imageView上显示图像? [英] How to display image on imageView selected from internal storage?
问题描述
我是第一次使用android中的图像.我想从内部存储加载图像,但它给了我权限被拒绝的错误,然后我将权限添加到了android清单文件中.但是我仍然无法完成任务.
I am new to working with images in android. I want to load image from internal storage but it is giving me permission denied error then i have added the permission to android manifest file. But still I am not able to complete my task.
这是我的代码:
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView image = (ImageView)findViewById(R.id.imageView);
Bitmap bm = BitmapFactory.decodeFile("/storage/emulated/0/DCIM/Camera/IMG_20151102_193132.jpg");
image.setImageBitmap(bm);
}
}
logcat:
01-16 15:16:11.345 6533-6533/com.example.jaytanna.imagemap E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/DCIM/Camera/IMG_20151102_193132.jpg: open failed: EACCES (Permission denied)
它不显示任何图像.请帮助我.谢谢.
It is not displaying any image. Kindly help me with this.Thank you.
推荐答案
选中此
File imgFile = new File("/storage/emulated/0/DCIM/Camera/IMG_20151102_193132.jpg");
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
};
并在清单文件中包含此权限:
And include this permission in the manifest file:
<manifest>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
<application>
...
<activity>
...
</activity>
</application>
</manifest>
对于API 23+,即使清单中已经存在读/写权限,您也需要请求读/写权限.
For API 23+ you need to request the read/write permissions even if they are already in your manifest.
// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
/**
* Checks if the app has permission to write to device storage
*
* If the app does not has permission then the user will be prompted to grant permissions
*
* @param activity
*/
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}
并处理响应,例如:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
有关请求API 23+权限的官方文档,请检查 https://developer. android.com/training/permissions/requesting.html
For official documentation about requesting permissions for API 23+, check https://developer.android.com/training/permissions/requesting.html
这篇关于如何在从内部存储器选择的imageView上显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!