简体   繁体   中英

Unable to decode stream: java.io.FileNotFoundException: sdcard/name/ c.jpg: open failed: EACCES (Permission denied)

I have the below in my MainActivity:

  public void clickImage(View view) {

    //fire intent

     Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
     File fp = getFile();
     intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(fp));
     startActivityForResult(intent, REQUEST_CHECK);

  }

 private File getFile() {
    File folder = new File("sdcard/attendence");
    if (!folder.exists()) {
        folder.mkdir();
    }
    imgpath = new File(folder, File.separator +

            Calendar.getInstance().getTime() + ".jpg");


    return imgpath;
 }

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


    try{
        s = imgpath.toString();
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }
    catch (Exception e){
        e.printStackTrace();

    }
    imgv1.setImageDrawable(Drawable.createFromPath(s));


}
public void sand(View view)
{
    db=mdb.getWritableDatabase();
    ContentValues cv =new ContentValues();
    cv.put("path" ,s);
    db.insert("tableimage", null, cv);
    Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();
}
public void show (View view) {
    String col[] = {"path"};
    db = mdb.getReadableDatabase();
    c = db.query("tableimage", col, null, null, null, null, null);

    c.moveToLast();

    imgs = c.getString(c.getColumnIndex("path"));

    imgv2.setImageDrawable(Drawable.createFromPath(imgs));
 }

I have included the read and write external storage in my manifest. When I try to get the image, it gives me the permission error (see title). Please help.

You need to add run-time permission request in Marshmallow or above android versions. Here is the code:

Declare File globally:

File fp;

Add run-time request before getting the file:

final int MyVersion = Build.VERSION.SDK_INT;

if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
       if (!checkIfAlreadyhavePermission()) {
           ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
       } else {
           vfp = getFile();
       }
 }

checkIfAlreadyhavePermission() method:

 private boolean checkIfAlreadyhavePermission() {
      int result = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
      return result == PackageManager.PERMISSION_GRANTED;
 }

Define ALLOW and DENY actions in onRequestPermissionResult():

@Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                   fp = getFile();

                } else {
                    Toast.makeText(MainActivity.this, "Please give your permission.", Toast.LENGTH_LONG).show();
                }
                break;
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM