2017-01-16 152 views
0

我使用Intent單擊圖像並啓動Android相機時出現問題。我通過意向數據得到的圖像攜帶調整大小的位圖圖像的信息。也許我有一個錯誤的理解,但請建議我能做些什麼來糾正它。該ImageView的顯示我點擊了相同的圖像,而是一個非常blurrred一個位圖圖像調整大小

這裏是底層代碼:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
InputStream stream= null; 
     super.onActivityResult(requestCode, resultCode, data); 
     if(this.requestCode == requestCode && resultCode == RESULT_OK){ 
      try{ 
       //stream= getContentResolver().openInputStream(data.getData()); 
       Bundle extras= data.getExtras(); 
       Bitmap bitmap = (Bitmap) extras.get("data"); 
       imageHolder.setImageBitmap(bitmap); 
      } 
      catch (Exception ex){ 
       ex.printStackTrace(); 
      } 
     } 
    } 
+0

使用bitmap.createScaledBitmap(); –

+0

爲什麼不使用uri而不是位圖? –

回答

0
 try{ 
      //stream= getContentResolver().openInputStream(data.getData()); 
      Bundle extras= data.getExtras(); 
      Bitmap bitmap = (Bitmap) extras.get("data"); 
      //set value Width=200, Height=200 
      Bitmap resizedimg = Bitmap.createScaledBitmap(bitmap, Width, Height, true); 

      imageHolder.setImageBitmap(resizedimg); 
     } 
     catch (Exception ex){ 
      ex.printStackTrace(); 
     } 
+0

即使使用bitmap.createScaledBitmap後,圖像仍然模糊不清 –

0

也許你應該通過一個URI來相機,並從中獲得圖像時ActivityResult代替位圖的

0

試試這個:

public class MainActivity extends Activity { 

    static int CAMERA_TAKE_PIC =1; 
    Uri outPutfileUri; 
    ImageView mImageView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mImageView = (ImageView) findViewById(R.id.image); 
    } 


    public void CameraClick(View v) { 

     Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     File file = new File(Environment.getExternalStorageDirectory(), 
       "Photo.jpg"); 
     outPutfileUri = Uri.fromFile(file); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri); 
     startActivityForResult(intent, CAMERA_TAKE_PIC); 
    } 

    Bitmap bitmap = null; 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode,Intent data) 
    { 
     if (requestCode == CAMERA_TAKE_PIC && resultCode==RESULT_OK) { 

      String uri = outPutfileUri.toString(); 
      Log.e("uri-:", uri); 
      Toast.makeText(this, outPutfileUri.toString(),Toast.LENGTH_LONG).show(); 

      //Bitmap myBitmap = BitmapFactory.decodeFile(uri); 
      // mImageView.setImageURI(Uri.parse(uri)); OR drawable make image strechable so try bleow also 

      try { 
       bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), outPutfileUri); 
       Drawable d = new BitmapDrawable(getResources(), bitmap); 
       mImageView.setImageDrawable(d); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 


     } 
} 

} 

我認爲形象是模糊的,因爲意圖數據僅包含圖像的thumbnail。您可以將圖像保存在內部/外部存儲上,然後獲取它。

0

圖像可能模糊,因爲意圖數據僅包含圖像的縮略圖。您必須將圖像保存在內部/外部存儲器中,然後獲取它。您的問題應該是如何從相機獲取全尺寸圖像。 閱讀本文檔https://developer.android.com/training/camera/photobasics.html

這是怎麼了應該做

Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
File dir= 
    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); 

output=new File(dir, "CameraContentDemo.jpeg"); 
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output)); 

startActivityForResult(i, CONTENT_REQUEST); 

Refrence可以在這個環節被發現。

https://stackoverflow.com/a/32121829/3111083