Reputation: 129
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
Upvotes: 8
Views: 39691
Reputation: 675
Try this get image file path form Bitmap
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();
}
}
Upvotes: 0
Reputation: 362
The android documentation has an example of defining a file URI for saving the output when you setup the Intent for your image capture.
Upvotes: -2
Reputation: 696
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
}
}
From Android documentation about android.provider.MediaStore.EXTRA_OUTPUT:
The name of the Intent-extra used to indicate a content resolver Uri to be used to store the requested image or video.
Upvotes: 8
Reputation: 9242
When you take your image, you must store it somewhere to get the location of the image.
To do this, you can store to the MediaStore:
String result = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "", "");
imageFileUri = Uri.parse(result);
Upvotes: 1
Reputation: 20319
You can try getting the URI of the image with
data.getExtras.get("URI");
but this might not work. What you can do is simply save the Bitmap yourself. See this question for a guide on how to do that: Save bitmap to location
Upvotes: 1