简体   繁体   English

存储图像时获取文件路径

[英]Obtaining File path when storing image

I'm trying to obtain the filepath of my stored Image that's stored like so after using the ACTION_IMAGE_CAPTURE intent: 我正在尝试使用ACTION_IMAGE_CAPTURE意图获取存储的存储图像的文件路径,如下所示:

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


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

}

SaveImageFunction 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();
    }
}

This way I can store this 'filepath' of the stored File to SharedPreferences to be accessed later on and say passed into the ACTION_SEND as an image attachment. 这样,我可以将存储的文件的“文件路径”存储到SharedPreferences中,以便稍后访问,并说作为图像附件传递到ACTION_SEND中。

after this line out.close(); 在这行之后out.close(); write this line 写这行

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

now use it how you want it 现在按需要使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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