好吧我正在创建一个Android应用程序,并使用CAPTURE_PIC_REQUEST拍摄照片并将其作为预览存储在屏幕上的imageviewer中。我想知道如何获取Uri和实际路径,因为我计划获取该信息,保存并稍后访问它。所以我无法想象如何从我刚刚拍摄的那张照片中获取Uri或路径名称。以下是我的活动中正在处理的代码
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);
}
}
}
那么我从应用程序的图像中获取文件位置
答案 0 :(得分:8)
据我所知,你可以这样做:
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.provider.MediaStore.EXTRA_OUTPUT的Android文档:
用于表示内容解析程序Uri的Intent-extra的名称 用于存储请求的图像或视频。
答案 1 :(得分:1)
您可以尝试使用
获取图像的URIdata.getExtras.get("URI");
但这可能不起作用。你可以做的只是自己保存位图。请参阅此问题以获取有关如何执行此操作的指南:Save bitmap to location
答案 2 :(得分:1)
拍摄图像时,必须将图像存储在某处以获取图像的位置。
为此,您可以存储到MediaStore:
String result = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "", "");
imageFileUri = Uri.parse(result);
答案 3 :(得分:0)
尝试此获取图像文件路径表单位图
public void uploadBitmap(Context mContext, Bitmap bitmap) {
String imagePath = null;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
Uri uri;
Cursor cursor = mContext.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, MediaStore.Images.Media.DATE_ADDED, null, "date_added DESC");
if (cursor != null && cursor.moveToFirst()) {
do {
uri = Uri.parse(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)));
imagePath = uri.toString();
Log.d("pathatka", uri.toString());
break;
} while (cursor.moveToNext());
cursor.close();
}
}
答案 4 :(得分:-2)