in my app the user can take an image from the camera intent and then i want to return that image to an image view. How can i do that?
Here is my camera intent:
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, TAKE_PICTURE);
And onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
//Check that request code matches ours:
if (requestCode == TAKE_PICTURE)
{
//Check if your application folder exists in the external storage, if not create it:
File imageStorageFolder = new File(Environment.getExternalStorageDirectory()+File.separator+"Kruger National Park");
if (!imageStorageFolder.exists())
{
imageStorageFolder.mkdirs();
Log.d(TAG , "Folder created at: "+imageStorageFolder.toString());
}
//Check if data in not null and extract the Bitmap:
if (data != null)
{
String filename = "image";
String fileNameExtension = ".jpg";
File sdCard = Environment.getExternalStorageDirectory();
String imageStorageFolder1 = File.separator+"Kruger National Park"+File.separator;
File destinationFile = new File(sdCard, imageStorageFolder1 + filename + fileNameExtension);
Log.d(TAG, "the destination for image file is: " + destinationFile );
if (data.getExtras() != null)
{
Bitmap bitmap = (Bitmap)data.getExtras().get("data");
try
{
FileOutputStream out = new FileOutputStream(destinationFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
}
catch (Exception e)
{
Log.e(TAG, "ERROR:" + e.toString());
}
That all works but just want to add it now to my ImageView:
ImageView image = (ImageView) v.findViewById(R.id.imageV);
image.setImageResource();
Could someone please help?
Here's an example activity that will launch the camera app and then retrieve the image and display it.
package edu.gvsu.cis.masl.camerademo;
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 MyCameraActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
} Note that the camera app itself gives you the ability to review/retake the image, and once an image is accepted, the activity displays it.
Here is the layout that the above activity uses. It is simply a LinearLayout containing a Button with id button1 and an ImageView with id imageview1:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/photo"></Button>
<ImageView android:id="@+id/imageView1" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_width="wrap_content"></ImageView>
</LinearLayout>
And one final detail, be sure to add:
<uses-feature android:name="android.hardware.camera"></uses-feature>
and if camera is optional to your app functionality. make sure to set require to false in the permission. like this
<uses-feature android:name="android.hardware.camera" android:required="false"></uses-feature>
to your manifest.xml.
Do you mean this?
image.setImageBitmap(bitmap);
To rotate the bitmap back to its original orientation, you can use the following code.
ExifInterface exif = new ExifInterface(path);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int angle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90)
angle = 90;
else if (orientation == ExifInterface.ORIENTATION_ROTATE_180)
angle = 180;
else if (orientation == ExifInterface.ORIENTATION_ROTATE_270)
angle = 270;
Matrix mat = new Matrix();
mat.postRotate(angle);
Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight, mat, true);
The String 'path' here is the path to the file where the picture was stored. It's the only code I have available at this time for that problem. I hope this will help you. What might be interesting in this case is that you can also give a file to the intent. The photo will be stored in that file.
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destinationFile));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.