0

This is my code for the image picker (both local and google photos)

This is my intent request

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    if(fileType!=null) {
        if(fileType.equals("video")) {
            photoPickerIntent.setType("video/*");
        }else if(fileType.equals("image"))
        {
            photoPickerIntent.setType("image/*");
        }
    }else
    {
        photoPickerIntent.setType("*/*");
    }
    startActivityForResult(photoPickerIntent, RESULT_LOAD_IMAGE);

This is my onActivityResult

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

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK) {


        Uri selectedImage = data.getData();


        if (selectedImage.contains("mediakey")){


                                InputStream is=null;
                                try {
                                    is = getContentResolver().openInputStream(Uri.parse(data.getData().toString()));
                                } catch (FileNotFoundException e) {
                                    e.printStackTrace();

                                }
                                    Bitmap bitmap = BitmapFactory.decodeStream(is);
                                    String tempSep="holder";

                                    File downloaded_file = null;
                                    try{
                                    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                                            Environment.DIRECTORY_PICTURES), "Plates");
                                    OutputStream fOut = null;
                                    downloaded_file = new File(mediaStorageDir.getPath() + File.separator +
                                            "IMG_" + tempSep + ".jpg");
                                    fOut = new FileOutputStream(downloaded_file);

                                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
                                    fOut.flush();
                                    fOut.close();

                                }catch (Exception e) {
                                    e.printStackTrace();
                                }
                                picturePath=downloaded_file.toString();
                                String extension = picturePath.substring(picturePath.lastIndexOf("."));
                                extension = extension.toLowerCase();
                                Log.e("Extension", extension);
                                if (extension.equals(".mp4") || extension.equals(".jpg") || extension.equals(".png") || extension.equals(".jpeg")) {

                                } else {

                                    imagePick();
                                    Toast.makeText(select_image_camera.this, "Wrong format !", Toast.LENGTH_SHORT).show();
                                    return;

                                }

        }else {//Process local images}

    }
    else {

        Intent i=new Intent(this,camera.class);
        i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(i);
    }
}

The above code works fine for most google cloud photos, but for some it fails and raises an exception when trying to download it through

InputStream is=null;
                            try {
                                is = getContentResolver().openInputStream(Uri.parse(data.getData().toString()));
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();

                            }

Is there something wrong with the code or something i could add? Thanks for helping

Sarthak Mishra
  • 1,033
  • 1
  • 11
  • 27
  • if you are targeting api level 25 or 26 you should use File provider . – umesh shakya Mar 09 '18 at 05:12
  • https://developer.android.com/reference/android/support/v4/content/FileProvider.html – umesh shakya Mar 09 '18 at 05:17
  • `openInputStream(Uri.parse(data.getData().toString()));`. That should be `openInputStream(data.getData());`. – greenapps Mar 09 '18 at 06:45
  • `catch (FileNotFoundException e) { e.printStackTrace(); }`. If there is a catch you should display a toast to the user. And then return. Now you continue as if nothing has happened. – greenapps Mar 09 '18 at 06:48
  • `Is there something wrong with the code`. Well it is pretty bad that you first make a bitmap from the inputstream and then compress the bitmap to that output stream. Do away with the bitmap. Instead make a loop where you read chunks of bytes from the input stream and write them directly to the outputstream. Will do for every type of file. – greenapps Mar 09 '18 at 06:52
  • Using a file provider is of course nonsense. – greenapps Mar 09 '18 at 06:53
  • @greenapps thanks for your comment ...true have to put a message and return in the catch statement.But, do you think adding openInputStream(data.getData()); would solve the problem... i mean will it make a big difference? :) – Sarthak Mishra Mar 09 '18 at 09:38
  • It is up to you to find out all. Try not closing the outputstream. – greenapps Mar 09 '18 at 09:47

0 Answers0