使用android lollipop,我想将相机图像设置为ImageView。
我已经尝试过,但我没有得到正确答案。 相机图像快速禁用。
这是我的代码:
package com.example.camera;
import java.io.File; import java.io.IOException;
import android.app.Activity; import android.content.Intent; import
android.graphics.Bitmap; import android.graphics.Matrix; import
android.media.ExifInterface; import android.net.Uri; import
android.os.Bundle; import android.os.Environment; import
android.provider.MediaStore; import android.view.Menu; import
android.view.View; import android.view.View.OnClickListener; import
android.widget.ImageView; import android.widget.Toast;
import com.example.cameraImageLoader.ImageLoader; //import
android.hardware.camera2.CameraAccessException; //import
android.hardware.camera2.CameraCaptureSession; //import
android.hardware.camera2.CameraDevice; //import
android.hardware.camera2.CaptureRequest; //import
android.hardware.camera2.CaptureResult; //import
android.hardware.camera2.TotalCaptureResult; //import
android.util.Size;
public class MainActivity extends Activity {
ImageView imgFavorite; private int PICTURE_RESULT = 0; String imagePath = ""; File profileImage; ImageLoader imageloader; Bitmap bm;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageloader = new ImageLoader(this); imgFavorite = (ImageView)
findViewById(R.id.imageView1); imgFavorite.setOnClickListener(new
OnClickListener() { @Override public void onClick(View v) { open(); }
});
}
public void open() { File f = new File(Environment.getExternalStorageDirectory() .getAbsoluteFile() + "/MyTempImage.jpg");
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); camera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(camera, PICTURE_RESULT);
}
@Override protected void onActivityResult(int requestCode, int
resultCode, Intent data) { // TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICTURE_RESULT)
{
if (resultCode == Activity.RESULT_OK) {
imagePath = Environment.getExternalStorageDirectory().getAbsoluteFile() + "/MyTempImage.jpg";
System.out.println("image path " + imagePath);
profileImage = new File(imagePath);
imageDisplay(profileImage, imagePath); }
}
}
@SuppressWarnings("static-access") public void imageDisplay(File profileImages, String paths) {
bm = imageloader.decodeFile(profileImages); System.out.println("bm > > is >>" + bm); if (bm != null) {Matrix matrix = new Matrix();
float rotation = MainActivity.rotationForImage(paths); if (rotation != 0f) {matrix.preRotate(rotation);}
bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true); imgFavorite.setImageBitmap(bm);} else {
Toast.makeText(getApplicationContext(), "Pick Images Only",
Toast.LENGTH_LONG).show(); }
}
@Override public boolean onCreateOptionsMenu(Menu menu) { //Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu); return true; }
public static float rotationForImage(String imagePath) { try { ExifInterface exif = new ExifInterface(imagePath);
int rotation = (int) exifOrientationToDegrees(exif.getAttributeInt( ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)); return rotation;}
catch (IOException e) { // Log.e(TAG, "Error checking exif", e); }
return 0f;
}
private static float exifOrientationToDegrees(int exifOrientation) {
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {return 90;}
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; }
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; }
return 0;
}
}
这是我的XML文件:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="110dip"
android:layout_height="100dip"
android:background="@drawable/ic_launcher"
android:scaleType="fitXY" >
</ImageView>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/imageView1"
android:text="tap"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
答案 0 :(得分:-1)
此路径将为您提供用于设置相机图像的imageUri。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String path;
Bitmap bitmap = null;
if (data == null)
return
path = Images.Media.insertImage(this.getContentResolver(),
(Bitmap) data.getExtras().get("data"), "Title", null);
}