我是新手,在android中使用图像.我想从内部存储加载图像但它给我权限被拒绝错误然后我已经添加了对androID清单文件的权限.但我仍然无法完成任务.
这是我的代码:
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)
它没有显示任何图像.请帮助我.谢谢.
解决方法:
检查一下
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); };
并在清单文件中包含此权限:
<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,您需要请求读/写权限,即使它们已经在您的清单中.
// Storage Permissionsprivate 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 ); }}
并处理响应,例如:
@OverrIDepublic 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
总结以上是内存溢出为你收集整理的android – 如何在imageView上显示从内部存储中选择的图像?全部内容,希望文章能够帮你解决android – 如何在imageView上显示从内部存储中选择的图像?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)