Android在图库中选择图片并在kitkat 4.4中以共享首选项设置路径

时间:2014-10-01 08:57:46

标签: android sharedpreferences android-4.4-kitkat android-gallery

我正在尝试关注:

  • 1)开启按钮点击从图库中选择图片
  • 2)将所选图像设置为imageview
  • 3)将Uri保存到共享首选项(当活动开始时i 检查所选图像的共享偏好值。如果存在然后设置 image to imageview)

检查路径的代码是否已经存储在共享首选项中,如果是,则将图像设置为imageview:

        sharedpreferences = getSharedPreferences(MyPREFERENCES,
                Context.MODE_APPEND);

        String uriImage = sharedpreferences.getString("imageUri", "");

        if (!uriImage.equals("")) 
        {

            if (Build.VERSION.SDK_INT < 19) {
                //No Problem Here
            }
            else 
            {
                try 
                {           

                      //error occur here
                    parcelFileDescriptor = getContentResolver().openFileDescriptor(Uri.parse(uriImage), "r");
                    FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
                    Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
                    parcelFileDescriptor.close();
                    fullImageView.setImageBitmap(image);


                } catch (FileNotFoundException e) 
                {
                    e.printStackTrace();
                } catch (IOException e) 
                {
                    e.printStackTrace();
                }
            }

        }

来自图库的摘要图片代码:

       btn.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,
                        "Select Picture"), SELECT_PICTURE);

            }
        });

图库的OnActivityResult代码

@SuppressLint("NewApi")
    protected void onActivityResult(int requestCode, int resultCode,
            Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);


        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = intent.getData();
                String a = selectedImageUri.getPath();

                if (Build.VERSION.SDK_INT < 19) {

                    try 
                    {
                        String imageUri = selectedImageUri.toString();
                        parcelFileDescriptor = getContentResolver().openFileDescriptor(selectedImageUri, "r");
                        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
                        Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
                        parcelFileDescriptor.close();
                        fullImageView.setImageBitmap(image);
                        catDialog.dismiss();
                        editor = sharedpreferences.edit();
                        editor.putString("imageUri", imageUri);

                        editor.commit();
                        editor.remove("AvailImagePath").commit();

                    } catch (FileNotFoundException e) 
                    {
                        e.printStackTrace();
                    } catch (IOException e) 
                    {
                        e.printStackTrace();
                    }

                }
                else 
                {
                    try 
                    {
                        String imageUri = selectedImageUri.toString();
                        parcelFileDescriptor = getContentResolver().openFileDescriptor(selectedImageUri, "r");
                        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();

                        Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
                        parcelFileDescriptor.close();
                        fullImageView.setImageBitmap(image);

                        editor = sharedpreferences.edit();
                        editor.putString("imageUri", imageUri);
                        editor.commit();
                        editor.remove("AvailImagePath").commit();

                    } catch (FileNotFoundException e) 
                    {
                        e.printStackTrace();
                    } catch (IOException e) 
                    {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

清单中的权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />

以上代码在旧版本中正常工作但问题仅在kitkat中出现

挑选图片时没有问题....它工作正常。

当我使用共享首选项值来设置图片时出现问题:当应用程序重新启动时

日志显示一些安全级别异常。

LOG:

10-01 14:44:01.750: D/AndroidRuntime(31363): Shutting down VM
10-01 14:44:01.750: W/dalvikvm(31363): threadid=1: thread exiting with uncaught exception (group=0x41fdad40)
10-01 14:44:01.752: E/AndroidRuntime(31363): FATAL EXCEPTION: main
10-01 14:44:01.752: E/AndroidRuntime(31363): Process: com.example.changeimage, PID: 31363
10-01 14:44:01.752: E/AndroidRuntime(31363): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.changeimage/com.example.changeimage.MainActivity}: java.lang.SecurityException: No permission grant found for UID 10146 and Uri content://com.android.providers.media.documents/document/image:5304
10-01 14:44:01.752: E/AndroidRuntime(31363):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
10-01 14:44:01.752: E/AndroidRuntime(31363):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
10-01 14:44:01.752: E/AndroidRuntime(31363):    at android.app.ActivityThread.access$800(ActivityThread.java:139)
10-01 14:44:01.752: E/AndroidRuntime(31363):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
10-01 14:44:01.752: E/AndroidRuntime(31363):    at android.os.Handler.dispatchMessage(Handler.java:102)
10-01 14:44:01.752: E/AndroidRuntime(31363):    at android.os.Looper.loop(Looper.java:136)
10-01 14:44:01.752: E/AndroidRuntime(31363):    at android.app.ActivityThread.main(ActivityThread.java:5086)
10-01 14:44:01.752: E/AndroidRuntime(31363):    at java.lang.reflect.Method.invokeNative(Native Method)
10-01 14:44:01.752: E/AndroidRuntime(31363):    at java.lang.reflect.Method.invoke(Method.java:515)
10-01 14:44:01.752: E/AndroidRuntime(31363):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-01 14:44:01.752: E/AndroidRuntime(31363):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-01 14:44:01.752: E/AndroidRuntime(31363):    at dalvik.system.NativeStart.main(Native Method)
10-01 14:44:01.752: E/AndroidRuntime(31363): Caused by: java.lang.SecurityException: No permission grant found for UID 10146 and Uri content://com.android.providers.media.documents/document/image:5304
10-01 14:44:01.752: E/AndroidRuntime(31363):    at android.os.Parcel.readException(Parcel.java:1465)
10-01 14:44:01.752: E/AndroidRuntime(31363):    at android.os.Parcel.readException(Parcel.java:1419)
10-01 14:44:01.752: E/AndroidRuntime(31363):    at android.app.ActivityManagerProxy.takePersistableUriPermission(ActivityManagerNative.java:3526)
10-01 14:44:01.752: E/AndroidRuntime(31363):    at android.content.ContentResolver.takePersistableUriPermission(ContentResolver.java:1641)
10-01 14:44:01.752: E/AndroidRuntime(31363):    at com.example.changeimage.MainActivity.onCreate(MainActivity.java:206)
10-01 14:44:01.752: E/AndroidRuntime(31363):    at android.app.Activity.performCreate(Activity.java:5248)
10-01 14:44:01.752: E/AndroidRuntime(31363):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
10-01 14:44:01.752: E/AndroidRuntime(31363):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
10-01 14:44:01.752: E/AndroidRuntime(31363):    ... 11 more
10-01 14:44:08.608: I/Process(31363): Sending signal. PID: 31363 SIG: 9

0 个答案:

没有答案