存储图像时获取文件路径

时间:2015-07-16 00:12:27

标签: java android android-intent sharedpreferences

我正在尝试获取我使用ACTION_IMAGE_CAPTURE意图后存储的存储Image的文件路径:

if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");


        mImageOne.setImageBitmap(imageBitmap);
        SaveImageOne(imageBitmap);

}

SaveImageFunction

private void SaveImageOne(Bitmap finalBitmap) {

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/saved_images");
    myDir.mkdirs();
    String fname = "Image-1.jpg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete ();
    try {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

这样我就可以将存储文件的'filepath'存储到SharedPreferences中,以便稍后访问,并说作为图像附件传递到ACTION_SEND。

1 个答案:

答案 0 :(得分:0)

在此行out.close();之后写下此行

String pathString = file.getAbsolutePath(); // gives you the path of the file

现在按照你想要的方式使用它