2012-06-05 60 views
1

我使用下面的onActivityResult代碼,但它不工作如何在android中顯示最近從相機捕獲的圖像?

File file1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),imagecapture); 

      if (file1.exists()) { 
       //Do action 
       image=(ImageView)findViewById(R.id.image); 
      Bitmap myBitmap = BitmapFactory.decodeFile(file1.getAbsolutePath()); 
      image.setImageBitmap(myBitmap); 
     image.invalidate(); 



      } 

但我不能明白爲什麼它不工作我也用 Android Camera Intent: how to get full sized photo? 但它給上線mImageUri = Uri.fromFile(photo); 任何錯誤幫助將被讚賞。

+1

您可以發佈您的logcat? – Shrikant

+0

上面的代碼沒有錯誤,但也沒有顯示圖像但是,當我使用它在oncreate它工作,但我希望它在onActivityResult – Nitin

+0

工作@Shrikant你可以給任何教程或任何其他提示如何才能實現相同 – Nitin

回答

2
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 MainActivity extends Activity { 
    /** Called when the activity is first created. */ 

    Button btnTakePhoto; 
    ImageView imgTakenPhoto; 
    private static final int CAM_REQUREST = 0; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     btnTakePhoto = (Button) findViewById(R.id.button1); 
     imgTakenPhoto = (ImageView) findViewById(R.id.imageView1); 

     btnTakePhoto.setOnClickListener(new btnTakePhotoClicker()); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // TODO Auto-generated method stub 
     super.onActivityResult(requestCode, resultCode, data); 

      if (requestCode == CAMERA_PIC_REQUEST) { 
       Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
       imgTakenPhoto.setImageBitmap(thumbnail); 
      } 
    } 

    class btnTakePhotoClicker implements Button.OnClickListener 
    { 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); 
     } 
    } 
} 
+0

有沒有錯誤,但有一段時間它顯示,但有些時候它沒有顯示圖像這真的很奇怪 – Nitin

+0

我認爲它給你的內存不足的問題,因爲圖像尺寸大 –

+0

你有任何解決方案或我應該提出另一個問題 – Nitin

0
File root=new File(Environment.getExternalStorageDirectory() 
       + File.separator + "MYPICS" + File.separator); 
File sdImageMainDirectory ; 
if (!root.exists()) { 
      root.mkdirs(); 
     } 
ImageButton cam =(ImageButton)findViewById(R.id.ImageButton1); 
cam.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       imageName = "MyPic" + i + ".png"; 
       sdImageMainDirectory = new File(root, imageName); 

       Uri imgUri = Uri.fromFile(sdImageMainDirectory); 

       Intent intent = new Intent(
         android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri); 

       startActivityForResult(intent, 0); 


      } 
     }); 




    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     switch (resultCode) { 
     case 0: 
      // finish(); 
      break; 

     case -1: 
      files = root.listFiles(); 
      count = files.length; 

      try { 
           BitmapFactory.Options option = new BitmapFactory.Options(); 
        option.inSampleSize = 2; 
      bm = BitmapFactory.decodeFile(root.getPath()+"/"+imageName,option); 

        ImageView imageview =(ImageView)findViewById(R.id.imageView1); 
        imageview .setImageBitmap(bm); 
    } 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
} 
+0

我無法得到哪個類的abject文件 – Nitin

+0

根和sdImageMainDirectory是文件類對象 –

+0

但相同的結果沒有圖像顯示在圖像視圖 – Nitin

相關問題