获取文件路径从ImageView的图片

问题描述:

好吧,我想提出一个Android应用程序,并使用CAPTURE_PIC_REQUEST拍照,并将其存储在一个图像浏览屏幕上的preVIEW。我想知道如何得到的URI和实际路径,因为我打算采取这一信息,将其保存以后访问它。所以,我不如何从那张照片我只是把得到的URI或路径名。下面是从我的活动,正在处理code

Alright I am making an Android app, and using the CAPTURE_PIC_REQUEST to take a picture and store it as a preview on the screen in an imageviewer. I wanted to know how to get the Uri and actually path because I am planning on taking that information, saving it and accessing it later. So I can't figure how to get the Uri or path name from that picture I just took. Here is the code from my activity that is handling

package com.CS480;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class AddEx extends Activity {
  static final int CAMERA_PIC_REQUEST = 1337; 
  public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add);

    Button camera = (Button) findViewById(R.id.picButton);
    camera.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);               
        }               
    });

}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAMERA_PIC_REQUEST) {  
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        ImageView image = (ImageView) findViewById(R.id.returnedPic);  
        image.setImageBitmap(thumbnail);  
    }  
}

}

因此​​,如何从我找回的应用程序,像做我得到的文件位置

So how from that image that I am getting back on the app do I get the file location

据我所知,你可以做这样的事情:

As far as I know, you can do something like this:

private Uri mImageCaptureUri;

public class AddEx extends Activity {
  static final int CAMERA_PIC_REQUEST = 1337; 
  public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add);

    Button camera = (Button) findViewById(R.id.picButton);
    camera.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Intent cameraIntent = 
                    new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                    mImageCaptureUri);
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);               
        }               
    });

}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAMERA_PIC_REQUEST) {  
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        ImageView image = (ImageView) findViewById(R.id.returnedPic);  
        image.setImageBitmap(thumbnail);  

        String pathToImage = mImageCaptureUri.getPath();

        // pathToImage is a path you need. 

        // If image file is not in there, 
        //  you can save it yourself manually with this code:
        File file = new File(mImageCaptureUri.getPath());
        FileOutputStream fOut = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut); // You can choose any format you want
    }  
}    

这是Android的文档中关于android.provider.MediaStore.EXTRA_OUTPUT:

From Android documentation about android.provider.MediaStore.EXTRA_OUTPUT:

表示一个内容解析URI的意向额外使用的名称   以用来存储所要求的图像或视频。

The name of the Intent-extra used to indicate a content resolver Uri to be used to store the requested image or video.