我是Android的首发,我正在做一个简单的应用程序,它能够将图像上传到服务器,我无法将图像从库中加载到ivImage图像视图中。这是我的代码,下面是错误。
public class Home extends AppCompatActivity implements View.OnClickListener{
private static final int RESULT_LOAD_IMAGE = 1;
ImageView ivUpload, ivGallery, ivImage;
EditText etItemdesc,etItemname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ivImage = (ImageView) findViewById(R.id.ivImage);
ivUpload = (ImageView) findViewById(R.id.ivUpload);
ivGallery = (ImageView) findViewById(R.id.ivGallery);
etItemdesc = (EditText) findViewById(R.id.etItemdesc);
etItemname = (EditText) findViewById(R.id.etItemname);
ivGallery.setOnClickListener(this);
ivUpload.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.ivGallery:
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent,RESULT_LOAD_IMAGE);
break;
case R.id.ivUpload:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode ==RESULT_OK && data!= null){
Uri selectedImage = data.getData();
ivImage.setImageURI(selectedImage); //error is on this row
}
}
}
这是错误
Unable to open content: content://media/external/images/media/42
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/42 from pid=3098, uid=10058 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at android.os.Parcel.readException(Parcel.java:1599)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)
at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:692)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1104)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:942)
at android.content.ContentResolver.openInputStream(ContentResolver.java:662)
at android.widget.ImageView.resolveUri(ImageView.java:832)
at android.widget.ImageView.setImageURI(ImageView.java:449)
at chengweifeng1132701116.fyp.Home.onActivityResult(Home.java:52)
at android.app.Activity.dispatchActivityResult(Activity.java:6428)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
您忘了添加此权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
如果您正在测试api20 +,请确保添加运行时许可。请参阅此处的文档:https://developer.android.com/training/permissions/requesting.html