the following is my camera code
public void takepic(View view) {
Spinner spinner1 = (Spinner)findViewById(R.id.spinner1);
String schname = spinner1.getSelectedItem().toString();
String[] tokens = schname.split(" ");
String timeStamp = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss").format(new Date());
String imageFileName = tokens[0] + "-" + timeStamp + ".jpg";
TextView detail = (TextView)findViewById(R.id.textView1);
detail.setText(imageFileName);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String name = imageFileName;
File file = new File(path, name );
ImageView mImage = (ImageView) findViewById(R.id.mImageView);
outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, TAKE_PICTURE);
new SingleMediaScanner(this, file);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
mImage.setImageBitmap(thumbnail);
}}}
when invoked it runs the camera app takes a photo and stores it in the Pictures folder with a pre defined filename
problem is it should then display the image in an imageview call mImageView but it doesnt
mImageView already has a bitmap in it. it should change to the new image but instead stays as it s
ouputFileURI reports the correct path and filename but I cant get it to work
Any ideas?
Mark
Since you are explicitly giving the file name where to store the image, you should read the Bitmap from that file rather than loading it from the Intent. It sounds like data.getExtras().get("data"); is returning null.
Related
In My app I am taking picture and I am successfully saving it in the gallery after compressing it. Now I want to show it into other activity, so that user can share it or view it at least. So How can I do that.
Following is my code which is saving picture and just after saving it, it shows ad , and on the adClosed event I want to send that taken picture to other activity , How Can I do that. My code just goes like this ..
File storagePath = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator + "MyAnimals");
storagePath.mkdirs();
String finalName = Long.toString(System.currentTimeMillis());
//this snippet is saving image And I am showing ad after saving picture
File myImage = new File(storagePath, finalName + ".jpg");
String photoPath = Environment.getExternalStorageDirectory().getAbsolutePath() +"/" + finalName + ".jpg";
try {
FileOutputStream fos = new FileOutputStream(myImage);
newImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
//refreshing gallery
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(Uri.fromFile(myImage));
sendBroadcast(mediaScanIntent);
} catch (IOException e) {
Toast.makeText(this, "Pic not saved", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this, "Pic saved in: " + photoPath, Toast.LENGTH_SHORT).show();
displayInterstitial();
interstitial.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
Log.v("Add time");
Intent intent = new Intent(CameraActivity.this,ShowCapturedImage.class);
//Now How to send the saved picture to the image view of other activity?
startActivity(intent);
super.onAdClosed();
}
});
1) put taken image path in intent
2) get path in other activity and set it in imageview
public static final int REQUEST_CODE_FROM_CAMERA = 112;
private Uri fileUri;
String image_path = "";
//Catch image from below function
private void fromCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
Log.d("FROM CAMERA CLICKED file uri", fileUri.getPath());
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, REQUEST_CODE_FROM_CAMERA);
}
//On Activity result store image path
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_FROM_CAMERA
&& resultCode == Activity.RESULT_OK) {
try {
image_path = fileUri.getPath();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}
On Click of any button
Intent iSecond=new Intent(FirstActivity.this,SecondActivity.class);
iSecond.putExtra("image_path",image_path);
startActivity(iSecond);
In Second Activity onCreate()
Bundle extras = intent.getExtras();
if(extras != null)
String image_path = extras.getString("image_path");
From this image path , You can get image and set to imageview
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.imageView1);
File imgFile = new File("/storage/emulated/0/1426484497.png");
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile
.getAbsolutePath());
iv.setImageBitmap(myBitmap);
}
}
You can do this by adding a ImageView to your Activity.
A simple ImageView should look like this:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView" />
Then you capture it in your Activity class
ImageView imageView = (ImageView) this.findViewById(R.id.imageView);
Now the fun part. We'll capture your image using the image URI and parse it in a bitmap.
Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath()); //Here goes your image path
imageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap,imageView.getWidth(), imageView.getHeight(), false)); //I scale the bitmap so it show properly. If the image is too big, it wont show on the ImageView
That should do the trick, tell me if it works!
I am trying to use the following code to take a picture with the camera and display it in a ImageView
public void takepic(View view) {
String timeStamp = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss").format(new Date());
String imageFileName = timeStamp + ".jpg";
TextView detail = (TextView)findViewById(R.id.textView1);
detail.setText(imageFileName);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String name = imageFileName;
File file = new File(path, name);
outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, TAKE_PICTURE);
}
static final int REQUEST_IMAGE_CAPTURE = 1;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case 3:
if (resultCode == RESULT_OK){
File imgFile = new File(outputFileUri.toString());
TextView detail = (TextView)findViewById(R.id.textView1);
detail.setText(imgFile.toString());
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.mImageView);
myImage.setImageBitmap(myBitmap);
}
else{
Toast.makeText(getBaseContext(), "Doesnt Exist", Toast.LENGTH_LONG).show();
}
}}}
Problem is its not displaying the picture
It takes the picture gives it a filename and saves it in the pictures directory.
when it gets to the onActivityResult outputFileUri.toString() gives the following
"file:/storage/emulated/0/Pictures/03-09-2014-06-53-04.jpg"
when i check the pictures directory the picture is there and is spelled correctly
but when it goes to the imgFile.exists if statement it says it doesn't exist and toasts the else toast
Any ideas where i'm going wrong?
any help appreciated
Mark
use this code:
if(imgFile!=null){
Bitmap myBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imgFile.toString());
ImageView myImage = (ImageView) findViewById(R.id.mImageView);
myImage.setImageBitmap(myBitmap);
}
In onActivityResult if your case is RESULT_OK, your parameter data would already be having the captured bitmap.
So you can try this:
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
Hope this helps.
I just want to save a picture in my Imagefolder in my phone.
I have got 2 examples which I tried.
1. Example
My app crashes when I activate the onClick Method:
public void onClick(View arg0) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1337);
}});
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if( requestCode == 1337)
{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
}
else
{
Toast.makeText(AndroidCamera.this, "Picture NOt taken", Toast.LENGTH_LONG);
}
super.onActivityResult(requestCode, resultCode, data);
}
2. Example
Before I saved my taken Picture with Uri. But it saved my picture in a folder, which I can only access on my PC or with a FileApp. I donĀ“t know how I can change the Path direction with Uri to my existing default image folder in my phone.
Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());
This is how I manage with saving images to specified imagefolder
When starting camera intent I define path and directory, where my image should be saved, and pass this as intetn extra when starting camera:
private void startCameraIntent() {
//create file path
final String photoStorePath = getProductPhotoDirectory().getAbsolutePath();
//create file uri
final Uri fileUri = getPhotoFileUri(photoStorePath);
//create camera intent
final Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//put file ure to intetn - this will tell camera where to save file with image
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start activity
startActivityForResult(cameraIntent, REQUEST_CODE_PHOTO_FROM_CAMERA);
//start image scanne to add photo to gallery
addProductPhotoToGallery(fileUri);
}
And here are some of helper methods used in code above
private File getProductPhotoDirectory() {
//get directory where file should be stored
return new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES),
"myPhotoDir");
}
private Uri getPhotoFileUri(final String photoStorePath) {
//timestamp used in file name
final String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.US).format(new Date());
// file uri with timestamp
final Uri fileUri = Uri.fromFile(new java.io.File(photoStorePath
+ java.io.File.separator + "IMG_" + timestamp + ".jpg"));
return fileUri;
}
private void addProductPhotoToGallery(Uri photoUri) {
//create media scanner intetnt
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
//set uri to scan
mediaScanIntent.setData(photoUri);
//start media scanner to discover new photo and display it in gallery
this.sendBroadcast(mediaScanIntent);
}
I have an Android app with an ImageButton. When user clicks on it, intent launches to show camera activity. When user capture the image, I'd like to save it in drawable folder of the app and display it in the same ImageButton clicked by the user, replacing the previous drawable image. I used the activity posted here: Capture Image from Camera and Display in Activity
...but when I capture an image, activity doesn't return to activity which contains ImageButton.
Edit code is:
public void manage_shop()
{
static final int CAMERA_REQUEST = 1888;
[...]
ImageView photo = (ImageView)findViewById(R.id.getimg);
photo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(camera, CAMERA_REQUEST);
}
});
[...]
}
And onActivityResult():
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
ImageButton getimage = (ImageButton)findViewById(R.id.getimg);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK)
{
Bitmap getphoto = (Bitmap) data.getExtras().get("data");
getimage.setImageBitmap(getphoto);
}
}
How can I also store the captured image in drawable folder?
once you've saved the image to a file you can use the following snippet to add to gallery.
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, new File(path).toString());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.Images.Media.DATA, path);
getContentResolver().insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI , values);
to save the file to a directory do the following
private saveFileToDir() {
final InputStream in = Wherever you input stream comes from;
File f = generatePhotoFile();
OutputStream out = new FileOutputStream(f);
byte[] buffer = new byte[1024];
int len;
while ((len=in.read(buffer))>0)
{
out.write(buffer,0,len);
}
in.close();
out.flush();
out.close();
}
private File generatePhotoFile() throws IOException {
Date date = new Date();
DateFormat df = new SimpleDateFormat("yyyymmdd_hhmmss");
String newPicFile = "IMG_"+ df.format(date) + ".jpg";
File f = new File(Environment.getExternalStorageDirectory()+"/DCIM/Camera/", newPicFile);
if (!f.exists())
{
if(!f.getParentFile().exists())
f.getParentFile().mkdir();
f.createNewFile();
}
return f;
}
How can I also store the captured image in drawable folder?
It's not possible to save an image to the drawable folder dynamically:
See:
android image save to res/drawable folder
and
Write to /res/drawable/ on the fly?
I am using the following code to take picture, using the device camera. I am new to android. Can anybody please help me and tell me where I should specify the path. I want to save images in a separate folder in sd card. Any help is deeply appreciated.
private static final int CAMERA_PIC_REQUEST = 2500;
bcontinue.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == CAMERA_PIC_REQUEST && resultCode==RESULT_OK)
{
try{
Byte image1 = (Byte) data.getExtras().get("data");
FileOutputStream fos = openFileOutput("filename.bmp", Context.MODE_PRIVATE);
fos.write(image1);
fos.close();
}
catch(Exception e){
}
Bitmap image = (Bitmap) data.getExtras().get("data");
ImageView imageview = (ImageView) findViewById(R.id.imageView1);
imageview.setImageBitmap(image);
Context context = getApplicationContext();
CharSequence text = "Click on the image!";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
The below code will start the default camera and have the camera save the image to the specified uri. The key is to put the extra "MediaStore.EXTRA_OUTPUT" along with the desired uri.
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/Images/" + image_name + ".jpg");
Uri imageUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, 0);