2014-01-13 158 views
1

我打開相機按鈕單擊並拍照並顯示它在imageview中。它在Google Nexus中工作。但它不適用於Samsung Tab和Micromax canvas HD,爲什麼?圖片不顯示在圖像查看

我的按鈕點擊代碼:

 CAMERA_PIC_REQUEST = 100; 

    String path = Environment.getExternalStorageDirectory() 
      + "/MySampleApp/image.jpg"; 

      File file = new File(path); 
      Uri outputFileUri = Uri.fromFile(file); 

      Intent cameraIntent = new Intent(
        android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
      startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); 

OnActivityResult代碼:

  BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inSampleSize = 4; 
       Bitmap bitmap = BitmapFactory.decodeFile(path, 
          options); 
        mImageView.setImageBitmap(bitmap); 

權限在manifest.xml中

<uses-permission android:name="android.permission.CAMERA" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-feature android:name="android.hardware.camera" /> 

爲什麼沒有在三星和Micromax的工作代碼?

這是正確的代碼是不是?

任何人請幫助我嗎?

+0

,能得到任何記錄中logcat的同時部署你的應用程序在三星標籤和Micromax帆布高清? –

+0

是的,錯誤從SDCard中讀取文件在android這個錯誤我得到 –

+0

錯誤解決或不?正如你接受了一個答案... –

回答

1

試試這個

btn.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View arg0) 
      { 
       CAMERA_PIC_REQUEST = 100; 
       Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); 
      } 
     }); 

而且

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
    super.onActivityResult(requestCode, resultCode, data); 
     if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) { 
     Bitmap photo = (Bitmap) data.getExtras().get("data"); 
     mImageView.setImageBitmap(photo); 
     } 
    } 
1

試試這個代碼將在工作福邦這devices..use URI

btnGallery.setOnClickListener(new OnClickListener() 
      { 

       @Override 
       public void onClick(View v) 
       { 
        Intent intent = new Intent(); 
        intent.setType("image/*"); 
        intent.setAction(Intent.ACTION_GET_CONTENT); 
        pop.dismiss(); 
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), StaticMembers.galleryRequestCode); 
       } 
      }); 
      ImageView btnCamera = (ImageView) pop.findViewById(R.id.ivCamera); 
      btnCamera.setOnClickListener(new OnClickListener() 
      { 

       @Override 
       public void onClick(View v) 
       { 

        //With Camera Utils 
        pop.dismiss(); 
        outpuUri = CameraUtil.startCam(yourActivity.this); 
       } 
      }); 


@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     Log.d("Alpha", "onActivity : " + requestCode); 
     System.gc(); 
     Log.d("Alpha", "onActivity : " + requestCode + " RESULT CODE : " + resultCode); 
     System.gc(); 
     long timestamp = System.currentTimeMillis()/1000L; 
     String time = timestamp + ""; 
     String imgPath = null; 

     if (requestCode == CameraUtil.IMAGE_CAPTURED && resultCode == Activity.RESULT_OK) 
     { 
      if (outpuUri != null) 
      { 
       Log.d("Alpha", "URI NOT NULL"); 
       imgPath = outpuUri.getPath(); 
       //Log.d("Alpha", "ACT RES PATH : " + imgPath); 
       //mCapturedBitmap = CameraUtil.sampleBitmap(imgPath, ivHeightWidth); 
       //iv.setImageBitmap(mCapturedBitmap); 
      } 
      else 
      { 
       Log.d("Alpha", "URI NULL IN CAM"); 
      } 
     } 
     else if (requestCode == StaticMembers.galleryRequestCode && resultCode == Activity.RESULT_OK) 
     { 
      outpuUri = data.getData(); 
      imgPath = getPath(outpuUri); 
      mCapturedBitmap = CameraUtil.sampleBitmap(imgPath, ivHeightWidth);// BitmapFactory.decodeFile(imgPath); 
      iv.setImageBitmap(mCapturedBitmap); 
      Log.d("Alpha", "In Gallery " + imgPath); 
     } 
     if (imgPath != null) 
     { 
      showConfrirmDialog(imgPath, time); 
     } 

    } 



    public class CameraUtil 
    { 

     private static Uri outpuUri; 

     public static final int IMAGE_CAPTURED = 200; 

     public static String imageName; 

     private static String imageFolder; 


     public static Uri startCam(Activity context) 
     { 
      imageName = "sample" + System.currentTimeMillis() + ".jpg"; 
      outpuUri = Uri.fromFile(new File(getImageFolderFile().getAbsolutePath() + File.separator + imageName)); 
      Log.d("CHECK", "BEFORE STARTING CAM URI : " + outpuUri.getPath()); 
      Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      i.putExtra(MediaStore.EXTRA_OUTPUT, outpuUri); 
      i.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
      context.startActivityForResult(i, IMAGE_CAPTURED); 
      return outpuUri; 
     } 


     public static File getImageFolderFile() 
     { 

      imageFolder = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "sparkchat_camera_pics"; 

      File f = new File(imageFolder); 
      Log.d("Alpha", f.getAbsolutePath() + " exists > " + f.exists()); 
      if (!f.exists()) 
       f.mkdirs(); 
      return f; 
     } 
    } 
+0

此代碼無法正常工作..圖像存儲在SD卡中,但是當我單擊SD卡中的圖像時,它會顯示我敬酒的物品無法找到。 –