适用于初学者的android studio中CRUD操作的清晰和最佳方法

时间:2014-10-16 17:00:02

标签: java android database sqlite

我正在学习Android开发,并从Johnny Manson youtube视频开始创建基本的CRUD应用程序。

在第6部分,他从图库中选择图像并设置ImageView URI。每一个都和视频一样,但是当我从图库中选择图像时,我的ImageView设置为空白

    _ContactImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Contact Image"), 1);
        }
    });
}

public void onActivityResult(int reqCode, int resCode, Intent data){
    if (resCode == RESULT_OK){
        if (reqCode == 1)
            _ContactImage.setImageURI(data.getData()); // I reach here when i Debug
    }

使用位图解决方案时的logcat

10-16 23:22:02.668  15504-15504/com.example.solutionwares.contactmanager E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.solutionwares.contactmanager, PID: 15504
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/67 (has extras) }} to activity {com.example.solutionwares.contactmanager/com.example.solutionwares.contactmanager.MyActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/67 from pid=15504, uid=10252 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3551)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3594)
            at android.app.ActivityThread.access$1400(ActivityThread.java:161)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/67 from pid=15504, uid=10252 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
            at android.os.Parcel.readException(Parcel.java:1465)
            at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
            at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:148)
            at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:682)
            at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1066)
            at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:907)
            at android.content.ContentResolver.openInputStream(ContentResolver.java:632)
            at com.example.solutionwares.contactmanager.MyActivity.onActivityResult(MyActivity.java:117)
            at android.app.Activity.dispatchActivityResult(Activity.java:5618)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3547)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3594)
            at android.app.ActivityThread.access$1400(ActivityThread.java:161)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)

只需在Manifest

中添加权限即可解决
</application>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
</manifest>

1 个答案:

答案 0 :(得分:0)

请尝试以下代码。这个建议的原因是,docs中有一些关于使用可能会导致打嗝的setImageURI的说明......

更新,示例onClick:

_ContactImage.setOnClickListener(new View.OnClickListener() {
    @Override
public void onClick(View view) {
     Intent intent = new Intent();
     intent.setType("image/*");
     intent.setAction(Intent.ACTION_GET_CONTENT);
     intent.addCategory(Intent.CATEGORY_OPENABLE);
     startActivityForResult(intent, 1)
    }
});

private Bitmap bitmap;

@Override
protected void onActivityResult(int reqCode, int resCode, Intent data) {
    if (reqCode== 1 && resCode == Activity.RESULT_OK) {
        try {

            if (bitmap != null) {
                bitmap.recycle();
            }

            InputStream stream = getContentResolver().openInputStream(
                    data.getData());
            bitmap = BitmapFactory.decodeStream(stream);
            stream.close();
            _ContactImage.setImageBitmap(bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    super.onActivityResult(reqCode, resCode, data);
 }

}