将所选图像的图像复制到另一个外部存储,但在这里获取错误,这是Android中的代码



画廊的图像选择

图像选择的目的:

 public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.setType("image/*");//for only image selection
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(Intent.createChooser(intent, "Select Picture"), IMAGE_REQUEST_CODE);
                    }

创建外部存储:

    FileOutputStream outputStream;

mFolder = new File(Environment.getExternalStorageDirectory(), "Yourfoldername");//create external storage directory
                if (!mFolder.exists()) {
                    mFolder.mkdirs();
                    mFolder.setExecutable(true);
                    mFolder.setReadable(true);
                    mFolder.setWritable(true);
                } //set permission also

意图数据的结果:

  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if(resultCode == RESULT_OK){
                if(requestCode == IMAGE_REQUEST_CODE){
                    fileUri = data.getData();
                    String s = getPath(fileUri);//getpath from function
                    Log.i(getClass().getName(), "fileUri" + fileUri);
                    try {
                        Toast.makeText(MainActivity.this,"hii",Toast.LENGTH_SHORT).show();
                        copyFile(s, mFolder + IMG_FILE_NAME+ ".jpg");//copy the image to selected folder
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }else{
                    Log.i(getClass().getName(), String.valueOf(fileUri));
                }
            }
        }

将图像从画廊复制到外部存储,copy.java:

public void copyFile(String selectedImagePath, String string) throws IOException {
            InputStream in = new FileInputStream(selectedImagePath);
            OutputStream out = new FileOutputStream(string);
            // Transfer bytes from in to out
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
            Toast customToast = new Toast(getBaseContext());
            customToast = Toast.makeText(getBaseContext(), "Image Transferred", Toast.LENGTH_LONG);
            customToast.setGravity(Gravity.CENTER|Gravity.CENTER, 0, 0);
            customToast.show();
        }
     public String getPath(Uri uri) {
         String[] projection = { MediaStore.Images.Media.DATA };
         Cursor cursor = managedQuery(uri, projection, null, null, null);
         startManagingCursor(cursor);
         int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
         cursor.moveToFirst();
         return cursor.getString(column_index);
     }// for get the path of image
    }

遇到相同的错误,我尝试了很多代码,无法修复此错误。

![获得这样的错误] [1]

[1]:https://i.stack.imgur.com/rfgyq.png

由您的stacktrace img,您的错误是:

Cursor cursor = managedQuery(uri, projection, null, null, null); 
startManagingCursor(cursor); // << Here 

缩略图seens是正确的

最新更新