2015-03-03 79 views
-2

使用android棒棒糖,我想將相機圖像設置爲ImageView。如何使用android棒棒糖將相機圖像設置爲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

PLZ把你的代碼.. – 2015-03-03 09:24:00

+0

@Finava,檢查, – Sugumaran 2015-03-03 09:29:15

回答

-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); 
} 
+0

在何處使用該代碼? – Sugumaran 2015-03-03 09:35:00

+0

OnActivity結果和設置在imageView.setImageUri(路徑) – Akash 2015-03-03 09:37:57

+0

沒有得到路徑 – Sugumaran 2015-03-03 09:42:05