Android Studio get File from Gallery Intent

故事扮演 提交于 2020-01-23 18:25:32

问题


Short answer is it possible to get original file from Gallery request,and if it possible how can i do it? This code doesn't work for me.

Uri uri = data.getData();
File file = new File(uri.getPath());

And the long Answer)): I use this code to make gallery intent

addGallery.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
             Intent intent = new Intent(Intent.ACTION_PICK);
             intent.setType("image/*");
             startActivityForResult(intent, GALLERY_IMAGE_REQUEST);
        }
    });

In mu onActivityResult i use this code

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK)
    {
        switch (requestCode)
        {
            case GALLERY_IMAGE_REQUEST:
                if (data != null)
                {
                    try
                    {
                        Uri uri = data.getData();
                        File file = new File(uri.getPath());
                        FileInputStream inputStream = new FileInputStream(file);
                        bitmap = BitmapFactory.decodeStream(inputStream);
                        imageView.setImageBitmap(bitmap);
                        inputStream.close();
                    } catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
        }
    }
}

And i cant get file.

The same code with getting bitmap from data works well but i need to get exactly file from gallery but not only Uri or Bitmap.

                 try
                    {
                        Uri uri = data.getData();
                        InputStream imageStream = getContentResolver().openInputStream(uri);
                        final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                        imageView.setImageBitmap(selectedImage);
                        imageStream.close();
                    } catch (Exception e)
                    {
                        e.printStackTrace();
                    }

回答1:


If you want to import a picture from gallery into your app (in a case your app own it), you need to copy it to your app data folder.

in your onActivityResult():

    if (requestCode == REQUEST_TAKE_PHOTO_FROM_GALLERY && resultCode == RESULT_OK) {
        try {
            // Creating file
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                Log.d(TAG, "Error occurred while creating the file");
            }

            InputStream inputStream = getActivity().getContentResolver().openInputStream(data.getData());
            FileOutputStream fileOutputStream = new FileOutputStream(photoFile);
            // Copying
            copyStream(inputStream, fileOutputStream);
            fileOutputStream.close();
            inputStream.close();

        } catch (Exception e) {
            Log.d(TAG, "onActivityResult: " + e.toString());
        }
    }

Creating the file method:

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

Copy method:

public static void copyStream(InputStream input, OutputStream output) throws IOException {
    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = input.read(buffer)) != -1) {
        output.write(buffer, 0, bytesRead);
    }
}


来源:https://stackoverflow.com/questions/50456342/android-studio-get-file-from-gallery-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!