且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

尝试在运行 4.2 的设备***问 Picasa 图像时出现安全异常

更新时间:2022-11-09 10:12:01

我最终解决这个问题的方法是将图像保存在本地,并在我需要图像的任何时候使用该图像的 uri

what I ended up doing to solve this is save the image locally and use the uri of that image anytime I need the image

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent){
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

            if(resultCode == Activity.RESULT_OK && requestCode == 1){  
                Uri selectedImage = imageReturnedIntent.getData();
                new SaveImage().execute(selectedImage.toString());

        }
    }

public class SaveImage extends AsyncTask<String,Void,Void>{

        @Override
        protected Void doInBackground(String... params) {
            String uri = params[0];

            if(uri != null && !uri.equals("")){
                ContextWrapper cw = new ContextWrapper(getActivity());
                File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
                File mypath=new File(directory,String.valueOf(bowlerID)+"profile.jpg");
                FileOutputStream fos = null;
                InputStream input = null;
                try { 
                    fos = new FileOutputStream(mypath);
                    input = getActivity().getContentResolver().openInputStream(Uri.parse(uri));
                    Bitmap bitmap = BitmapFactory.decodeStream(input);
                    if(bitmap != null){
                        if(bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos)){
                            ContentValues values = new ContentValues();
                            values.put(BowlersDB.PHOTO_URI, mypath.getAbsolutePath());
                            getActivity().getContentResolver().update(BowlersDB.CONTENT_URI, values,BowlersDB.ID+"="+bowlerID,null);
                        }
                    }
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

    }