2013-04-24 135 views
2

我打電話給相機目的並嘗試在SD卡中保存捕獲圖像,但相機意圖給我空數據。我不能理解的問題是什麼。我的代碼如下在SD卡中保存捕獲圖像android

imgBtnPicture.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      String fileName="ashraful.jpg"; 

      String root = Environment.getExternalStorageDirectory().toString(); 
      new File(root + "/photofolder").mkdirs(); 
      File outputfile=new File(root+"/photofolder/", fileName); 
      Uri outputFileUri = Uri.fromFile(outputfile); 
      Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
      startActivityForResult(cameraIntent, CAMERA_REQUEST); 
     } 
    }); 


protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 

     if(data!=null) 
     { 
      Bitmap photo = (Bitmap) data.getExtras().get("data"); 
      Bitmap icon1=ResizeBitmap(photo,200,200); 
      imgBtnPicture.setImageBitmap(icon1); 
     } 
    } 
} 

但我得到空數據?如何解決我的問題?plz幫助

回答

1

this的Android培訓仔細崗位理出頭緒。正如帖子所言,

如果您爲其保存文件,Android Camera應用程序會保存一張全尺寸的照片。

所以如果你沒有通過文件的Uri,你應該使用這個Uri訪問圖像。

2
private void takeNewPicture() { 

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    ContentValues values = new ContentValues(3); 

    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); 
    cameraImagePath = getContentResolver().insert(
      MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraImagePath); 
    startActivityForResult(takePictureIntent, CAMERA_REQUEST); 
} 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 

    String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
    Uri imageFilePathUri = null; 
    if (cameraImagePath != null) { 
     Cursor imageCursor = getContentResolver().query(
      cameraImagePath, filePathColumn, null, null, null); 
    if (imageCursor != null && imageCursor.moveToFirst()) { 
     int columnIndex = imageCursor.getColumnIndex(filePathColumn[0]); 
     String filePath = imageCursor.getString(columnIndex); 
     imageCursor.close(); 
     imageFilePathUri = filePath != null ? Uri 
       .parse(filePath) : null; 
    } 

    } 


    } 
} 

cameraImagePath是一個Uri對象