2

When I run my app on android 7 I the app crashes. Android monitor throws this :

Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/picture.jpg exposed beyond app through ClipData.Item.getUri()

And code is

   @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            switch (requestCode) {
                case REQUEST_WRITE_PERMISSION:
                    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        takePicture();
                    } else {
                        Toast.makeText(MainActivity.this, "Permission Denied!", Toast.LENGTH_SHORT).show();
                    }
            }
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == PHOTO_REQUEST && resultCode == RESULT_OK) {
                Toast.makeText(this,"CLICK ON THE TEXT TO SAVE THE DATA",Toast.LENGTH_LONG).show();
                launchMediaScanIntent();
                try {
                    Bitmap bitmap = decodeBitmapUri(this, imageUri);
                    if (detector.isOperational() && bitmap != null) {
                        Frame frame = new Frame.Builder().setBitmap(bitmap).build();
                        SparseArray<TextBlock> textBlocks = detector.detect(frame);
                        String blocks = "";
                        String lines = "";
                        String words = "";
                        for (int index = 0; index < textBlocks.size(); index++) {
                            //extract scanned text blocks here
                            TextBlock tBlock = textBlocks.valueAt(index);
                            blocks = blocks + tBlock.getValue() + "\n" + "\n";
                            for (Text line : tBlock.getComponents()) {
                                //extract scanned text lines here
                                lines = lines + line.getValue() + "\n";
                                for (Text element : line.getComponents()) {
                                    //extract scanned text words here
                                    words = words + element.getValue() + ", ";
                                }
                            }
                        }
                        if (textBlocks.size() == 0) {

                            scanResults.setText("Scan Failed: Found nothing to scan");
                        } else {
                            scanResults.setText(scanResults.getText() +   "\n");
                            scanResults.setText(scanResults.getText() + blocks + "\n");

                        }
                    } else {
                        scanResults.setText("Could not set up the detector!");
                    }
                } catch (Exception e) {
                    Toast.makeText(this, "Failed to load Image", Toast.LENGTH_SHORT)
                            .show();
                    Log.e(LOG_TAG, e.toString());
                }
            }
        }

        private void takePicture() {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File photo = new File(Environment.getExternalStorageDirectory(), "picture.jpg");
            imageUri = Uri.fromFile(photo);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            startActivityForResult(intent, PHOTO_REQUEST);
            scanResults.setText("");
        }

        @Override
        protected void onSaveInstanceState(Bundle outState) {
            if (imageUri != null) {
                outState.putString(SAVED_INSTANCE_URI, imageUri.toString());
                outState.putString(SAVED_INSTANCE_RESULT, scanResults.getText().toString());
            }
            super.onSaveInstanceState(outState);
        }

        private void launchMediaScanIntent() {
            Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            mediaScanIntent.setData(imageUri);
            this.sendBroadcast(mediaScanIntent);
        }

        private Bitmap decodeBitmapUri(Context ctx, Uri uri) throws FileNotFoundException {
            int targetW = 600;
            int targetH = 600;
            BitmapFactory.Options bmOptions = new BitmapFactory.Options();
            bmOptions.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(ctx.getContentResolver().openInputStream(uri), null, bmOptions);
            int photoW = bmOptions.outWidth;
            int photoH = bmOptions.outHeight;

            int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
            bmOptions.inJustDecodeBounds = false;
            bmOptions.inSampleSize = scaleFactor;

            return BitmapFactory.decodeStream(ctx.getContentResolver()
                    .openInputStream(uri), null, bmOptions);
        }

        public void store_in_database(View view) {

            Intent i = new Intent(MainActivity.this , DbActivity.class);
            i.putExtra("message",scanResults.getText().toString());
            MainActivity.this.startActivity(i);


        }

Any help in the form of code would be great. I understand this may be due to the file system permissions related to android 7 . Any explanation in the form of code is most welcomed. I am trying to access the camera and store the image into android database.

Nayan Srivastava
  • 3,655
  • 3
  • 27
  • 49
TestStart
  • 93
  • 1
  • 9

3 Answers3

0

In nougat what you have to do is use FileProvider for path like this:

  1. declare provider in manifest under application:

      <application
    
               ...>
                   <provider
                        android:name="android.support.v4.content.FileProvider"
                         android:authorities="com.safetynetwork.provider"
                        android:exported="false"
                         android:grantUriPermissions="true">
                         <meta-data
                            android:name="android.support.FILE_PROVIDER_PATHS"
                            android:resource="@xml/provider_paths" />
                     </provider>
                   </application>
    
  2. Create xml package under resource and add provider_path.xml there.

    <paths>
        <external-path name="SN" path="Android/data/com.packagename/files/Pictures/SN/"/>
    </paths>
    
  3. now set path for image that you mention in provider_path.xml

    public Uri getOutputMediaFileUri(int type) {
        return Uri.fromFile(getOutputMediaFile(type));
    }
    
    /**
     * returning image / video
     */
    private File getOutputMediaFile(int type) {
    
        // External sdcard location
        File mediaStorageDir = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES),
                "SN");
    
        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d("FileName::", "Oops! Failed create "
                        + "SN" + " directory");
                return null;
            }
        }
    
        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                Locale.getDefault()).format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator
                    + "IMG_" + timeStamp + ".jpg");
        } else {
            return null;
        }
    
        mCurrentPhotoPath = mediaFile.getAbsolutePath();
        return mediaFile;
    }
    
  4. now pick photo like this:

    private void selectImage() {

        final CharSequence[] options = {"Camera", "Choose from Gallery"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Add Photo!");
        builder.setItems(options, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (options[item].equals("Camera")) {
                    isCamera = 1;
                    if (checkandRequestPermission()) {
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                            fileUri = FileProvider.getUriForFile(ActivityModifyProfile.this, getPackageName() + ".provider", getOutputMediaFile(MEDIA_TYPE_IMAGE));
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
                        } else {
                            fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
                        }
                        startActivityForResult(intent, 1);
                    }
    
                } else if (options[item].equals("Choose from Gallery")) {
                    isCamera = 0;
                    if (checkandRequestPermission()) {
                        Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(intent, 2);
                    }
                }
            }
        });
        builder.show();
    }
    
  5. set path to bitmap like this:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                Log.d("myPath1:", mCurrentPhotoPath);
                Log.d("myPath2:", fileUri.getPath());
                try {
                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        bitmap1 = BitmapFactory.decodeFile(mCurrentPhotoPath, bitmapOptions);
                    } else {
                        bitmap1 = BitmapFactory.decodeFile(fileUri.getPath(), bitmapOptions);
                    }
                    imgProfile.setImageBitmap(bitmap1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (requestCode == 2) {
                Uri selectedImage = data.getData();
                String[] filePath = {MediaStore.Images.Media.DATA};
                Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
                c.moveToFirst();
                int columnIndex = c.getColumnIndex(filePath[0]);
                imagePath = c.getString(columnIndex);
                c.close();
                bitmap1 = (BitmapFactory.decodeFile(imagePath));
                imgProfile.setImageBitmap(bitmap1);
            }
        }
    }
Sagar Chavada
  • 5,169
  • 7
  • 40
  • 67
0

Add this code in MainActivity.java in oncreate()

 if (ContextCompat.checkSelfPermission(GoogleMapsActivity.this,
            android.Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if ActivityCompat.shouldShowRequestPermissionRationale(GoogleMapsActivity.this,
                android.Manifest.permission.READ_EXTERNAL_STORAGE)) {

        } else {

            ActivityCompat.requestPermissions(GoogleMapsActivity.this,
                    new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        }
    }
Kishor N R
  • 1,521
  • 1
  • 17
  • 23
0

Finally I got the app running successfully .

1.Declare provider in manifest in application.

 <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.example.abhijeet.demo11.MainActivity.GenericFileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>

2 . Create xml package under resource and add provider_path.xml there.

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

3 . Change

 imageUri = Uri.fromFile(photo);

to

  imageUri = FileProvider.getUriForFile(getApplicationContext(),"com.example.abhijeet.demo11.MainActivity.GenericFileProvider",photo);
TestStart
  • 93
  • 1
  • 9