无法使用ACTION_GET_CONTENT复制图像

时间:2016-03-23 07:09:35

标签: android android-intent storage android-image

我正在尝试使用以下代码复制图片:

  Intent intentImage = new Intent();
        intentImage.setType("image/*");
        intentImage.setAction(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intentImage, 10); 

通过此功能,我可以打开所有图像内容。

   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {


        if (requestCode == 10) {
            if (resultCode != RESULT_OK) return;
            Uri selectedImageUri = data.getData();
            try {

                String selectedImagePath1 = getPath(selectedImageUri);

                File file = new File(selectedImagePath1);
                String fna = file.getName();
                String pna = file.getParent();
                File fileImage = new File(pna, fna);

                copyFileImage(fileImage, data.getData());

            } catch (Exception e) {

            }

        }
    }


private void copyFileImage(File src, Uri destUri) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            bis = new BufferedInputStream(new FileInputStream(src));
            bos = new BufferedOutputStream(getContentResolver().openOutputStream(destUri));
            byte[] buf = new byte[1024];
            bis.read(buf);
            do {
                bos.write(buf);
            } while (bis.read(buf) != -1);
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bis != null) bis.close();
                if (bos != null) bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

现在我成功获取了图片的路径和名称。

现在,当我运行上面的代码时,它会给我一个需要android.permission.MANAGE_DOCUMENTS, or grantUriPermission()的错误。

所以我把权限放在了清单中:

我还定义了读写内部/外部存储的权限。

但我仍然收到此错误。

我如何复制图像?

2 个答案:

答案 0 :(得分:0)

Select picture using below code 

 Intent intent = new Intent();
             intent.setType("image/*");
             intent.setAction(Intent.ACTION_GET_CONTENT);
             startActivityForResult(Intent.createChooser(intent,
                     "Select Picture"), 1);


this will open gallery, after selecting pic you will get selected pic uri in below code

 protected void onActivityResult(int requestCode, int resultCode, Intent data)
     {
     switch(requestCode) {

     case 1:
         if(resultCode == RESULT_OK)
         {
             Uri selectedImageUri = data.getData();
             String selectedImagePath = getPath(selectedImageUri);  
             File sel = new File(selectedImagePath);
             Bitmap bitmap = BitmapFactory.decodeFile(sel.getAbsolutePath());
             imageView1.setImageBitmap(bitmap);


             Bitmap resized = Bitmap.createScaledBitmap(bitmap, 600,370, true);

                ByteArrayOutputStream blob = new ByteArrayOutputStream();
                resized.compress(Bitmap.CompressFormat.JPEG, 100, blob);
                String StrBase64 = Base64.encodeToString(blob.toByteArray(), Base64.DEFAULT);

                imageView1.setImageBitmap(resized);

          //   Toast.makeText(getApplicationContext(), ""+selectedImagePath, Toast.LENGTH_LONG).show();
         }
         break;
     }
     }

      public String getPath(Uri uri) {
             // just some safety built in 
             if( uri == null ) {
                 // TODO perform some logging or show user feedback
                 return null;
             }
             // try to retrieve the image from the media store first
             // this will only work for images selected from gallery
             String[] projection = { MediaStore.Images.Media.DATA };
             Cursor cursor = managedQuery(uri, projection, null, null, null);
             if( cursor != null ){
                 int column_index = cursor
                 .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                 cursor.moveToFirst();
                 return cursor.getString(column_index);
             }
             // this is our fallback here
             return uri.getPath();
     }


add permission in manifest  
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

this way you will get selected image in Base64 to string

答案 1 :(得分:0)

Try this code-
Image will copy in SaveImage folder in sd card

  protected void onActivityResult(int requestCode, int resultCode, Intent data)
         {
         switch(requestCode) {

         case 1:
             if(resultCode == RESULT_OK)
             {
                 Uri selectedImageUri = data.getData();
                 String selectedImagePath = getPath(selectedImageUri);  
                 File sel = new File(selectedImagePath);
                 Bitmap bitmap = BitmapFactory.decodeFile(sel.getAbsolutePath());
                 imageView1.setImageBitmap(bitmap);


                 SaveImage(bitmap);
             }
             break;
         }
         }

      private void SaveImage(Bitmap finalBitmap) {

           String root = Environment.getExternalStorageDirectory().toString();
           File myDir = new File(root + "/SaveImage");    
           myDir.mkdirs();
           Random generator = new Random();
           int n = 10000;
           n = generator.nextInt(n);
           String fname = "Image-"+ n +".jpg";
           File file = new File (myDir, fname);
           if (file.exists ()) file.delete (); 
           try {
               FileOutputStream out = new FileOutputStream(file);
               finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
               out.flush();
               out.close();

           } catch (Exception e) {
               e.printStackTrace();
           }
        }