2013-03-05 40 views
0

我是Android應用程序開發的新手。我正在嘗試使用Intents拍攝照片。一切正常,我可以拍照並將其保存到外部存儲器,但我無法獲得它保存的位置。 這裏是我的onCreate方法:onActivityResult的數據始終爲空

private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100; 
public static final int MEDIA_TYPE_IMAGE = 1; 
protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 
     startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 
    } 

在這裏,我創建的路徑保存圖片:

private static Uri getOutputMediaFileUri(int type){ 
      return Uri.fromFile(getOutputMediaFile(type)); 
    } 


private static File getOutputMediaFile(int type){ 
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
       Environment.DIRECTORY_PICTURES), "MyCameraApp"); 

    if (! mediaStorageDir.exists()){ 
     if (! mediaStorageDir.mkdirs()){ 
      Log.d("MyCameraApp", "failed to create directory"); 
      return null; 
     } 
    } 

    // Create a media file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    File mediaFile; 
    if (type == MEDIA_TYPE_IMAGE){ 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator + 
     "IMG_"+ timeStamp + ".jpg"); 
    } else { 
     return null; 
    } 

    return mediaFile; 
} 

圖像保存在指定的路徑,但在onActivityResult數據爲null,我可以」噸得到的路徑:

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) 
     { 
      if(resultCode == RESULT_OK){ 
       Toast.makeText(this, "Video saved to:\n" +data.getData(), Toast.LENGTH_LONG).show(); 

      } 

     } 
    } 
+0

也許你的相機返回'URI'而不是'數據'? – Skynet 2013-03-05 11:23:37

+0

片段或活動中的OnActivityResult方法?如果它在一個片段中,則嘗試將其設置在活動中,並查看它是否有效。 – Luciano 2013-03-05 11:34:06

+0

@Luizje - > no,在活動中。謝謝 – saeed 2013-03-05 12:13:20

回答

1

使用此功能,請單擊圖像

public void takePhoto1() { 
     if (!android.os.Environment.getExternalStorageState().equals(
       android.os.Environment.MEDIA_MOUNTED)) { 
      Toast.makeText(Add_View_Images_Activity.this, 
        "Please insert SDcard for capturing photo.", 
        Toast.LENGTH_SHORT).show(); 
     } else { 

      try { 

       photo1=new File(fWrapper.path+"/"+System.currentTimeMillis()+".jpg"); 
       Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    
       cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo1)); 
       cameraIntent.putExtra("return-data", true); 

       startActivityForResult(cameraIntent, 4); 
      } catch (Exception e) { 
       Toast.makeText(Add_View_Images_Activity.this, ""+e, Toast.LENGTH_LONG).show(); 
      } 
     } 

    } 

此功能將圖像保存在指定位置。 現在,您可以從onActivityResult中的文件photo1的路徑中獲取點擊圖像的路徑。

這樣

String path=photo1.getAbsolutePath(); 

現在只是通過你得到這個功能,它所有的時間工作100%的路徑。

public Bitmap getImage(String path) throws IOException 
     { 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inJustDecodeBounds = true; 
      BitmapFactory.decodeFile(path, options);   
      int srcWidth = options.outWidth; 
      int srcHeight = options.outHeight; 
      int[] newWH = new int[2]; 
      newWH[0] = srcWidth/2; 
      newWH[1] = (newWH[0]*srcHeight)/srcWidth; 

      int inSampleSize = 1; 
      while(srcWidth/2 >= newWH[0]){ 
       srcWidth /= 2; 
       srcHeight /= 2; 
       inSampleSize *= 2; 
      } 

      //  float desiredScale = (float) newWH[0]/srcWidth; 
      // Decode with inSampleSize 
      options.inJustDecodeBounds = false; 
      options.inDither = false; 
      options.inSampleSize = inSampleSize; 
      options.inScaled = false; 
      options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
      Bitmap sampledSrcBitmap = BitmapFactory.decodeFile(path,options); 
      ExifInterface exif = new ExifInterface(path); 
      String s=exif.getAttribute(ExifInterface.TAG_ORIENTATION); 
      System.out.println("Orientation>>>>>>>>>>>>>>>>>>>>"+s); 
      Matrix matrix = new Matrix(); 
      float rotation = rotationForImage(Add_View_Images_Activity.this, Uri.fromFile(new File(path))); 
      if (rotation != 0f) { 
       matrix.preRotate(rotation); 
      } 
      int newh = (w * sampledSrcBitmap.getHeight()) /sampledSrcBitmap.getWidth(); 
      Bitmap r=Bitmap.createScaledBitmap(sampledSrcBitmap, w, newh, true); 
      Bitmap resizedBitmap = Bitmap.createBitmap(
        r, 0, 0, w, newh, matrix, true); 

      return resizedBitmap; 
     }