检索照片时出现路径问题

Paths Issue while retrieving a photo

我在拍摄照片并将其设置为 ImageView 时遇到路径问题。 当我使用这个方法时

File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

使用的路径是

02-01 12:07:24.068 31618-31618/disco.unimib.it.polapp D/ciao: /storage/emulated/0/Android/data/disco.unimib.it.polapp/files/Pictures

但是当我像这样分配 uri 时

photoUri = FileProvider.getUriForFile(NotifyActivity.this, "com.example.android.fileprovider", photoFile);

uri 显示

02-01 12:07:24.071 31618-31618/disco.unimib.it.polapp D/ciao: content://com.example.android.fileprovider/images/Android/data/disco.unimib.it.polapp/files/Pictures/JPEG_20180201_120724_1953425230.jpg

当我尝试将照片保存到变量时,错误是

02-01 12:07:29.975 31618-31618/disco.unimib.it.polapp E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /images/Android/data/disco.unimib.it.polapp/files/Pictures/JPEG_20180201_120724_1953425230.jpg (No such file or directory)

这是完整的代码

NotifyActivity.java:

public class NotifyActivity extends AppCompatActivity {

Bitmap image;

String mCurrentPhotoPath;

File photoFile=null;

Uri photoUri;

Intent TakePictureIntent;

ImageView photoSaved;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notify);


    final Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar4);
    setSupportActionBar(toolbar);
    setTitle(R.string.problem);

    final Button button3=(Button) findViewById(R.id.button3);
    final Button button4=(Button) findViewById(R.id.button4);
    photoSaved=(ImageView) findViewById(R.id.photoSaved);

    button4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TakePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (TakePictureIntent.resolveActivity(getPackageManager()) != null) {
                if (ContextCompat.checkSelfPermission(NotifyActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(NotifyActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
                } else {
                    try {
                        photoFile = createImageFile();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    if (photoFile != null) {
                        photoUri = FileProvider.getUriForFile(NotifyActivity.this, "com.example.android.fileprovider", photoFile);
                        Log.d("ciao", String.valueOf(photoUri));
                        TakePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
                        startActivityForResult(TakePictureIntent, 50);
                    }

                }


            }
        }
    });

    button3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Snackbar.make(findViewById(R.id.notify),R.string.notificationsent,Snackbar.LENGTH_LONG);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    image= BitmapFactory.decodeFile(photoUri.getPath());
    if(image!=null){
        Snackbar.make(findViewById(R.id.notify),R.string.photosaved,Snackbar.LENGTH_LONG).show();
        photoSaved.setImageBitmap(image);
    }
}

private File createImageFile() throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    Log.d("ciao", String.valueOf(getExternalFilesDir(Environment.DIRECTORY_PICTURES)));
    File image = File.createTempFile(imageFileName, ".jpg", storageDir);
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

@Override
public void onRequestPermissionsResult(int requestCode, String [] permissions,int [] grantResults){
    if (grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
        try{
            photoFile = createImageFile();
        } catch(IOException ex){
            ex.printStackTrace();
        }
        if (photoFile != null) {
            photoUri = FileProvider.getUriForFile(NotifyActivity.this, "com.example.android.fileprovider", photoFile);
            TakePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
            startActivityForResult(TakePictureIntent, 50);
        }
    }else{
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

}

file_paths.xml:

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

我已经把供应商放在我的 androidmanifest.xml

<external-path。更改为 <external-files-path

image= BitmapFactory.decodeFile(photoUri.getPath());

替换为

InputStream is = getContentResolver().openInputStream(photoUri);
image= BitmapFactory.decodeStream(is);

java.io.FileNotFoundException 被抛出是因为您正在使用 FileProvider 的 Uri,它创建

content:// Uri for a file.

BitmapFactory.decodeFile(String path) 需要

complete path name for the file to be decoded.

因此,您需要使用:

BitmapFactory.decodeFile(photoFile.getAbsolutePath());