2015-12-03 65 views
0

我正試圖在設備上保存捕獲的圖像。但它不會被保存。捕獲的圖像沒有保存在設備上

// ---清單

<uses-permission android:name="ANDROID.PERMISSION.CAMERA" /> 
<uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE" /> 

我使用下面的代碼:

if (resultCode == RESULT_OK) { 
     if (requestCode == CAMERA_REQUEST) { 
      Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
      ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
      thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes); 

      try { 
       Bitmap photo = (Bitmap) data.getExtras().get("data"); 
       File outFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "myimage.jpg"); 
       FileOutputStream fos = new FileOutputStream(outFile); 
       photo.compress(Bitmap.CompressFormat.JPEG, 100, fos); 

       fos.flush(); 
       fos.close(); 
       getImages(); 
+0

您是否包含'WRITE_EXTERNAL_STORAGE'權限? –

+0

你有清單中的權限嗎? – KDeogharkar

+0

你有GOOGLE了它 –

回答

0

看到我在我的代碼已經做用於保存拍攝的圖像。

/*************************** Camera Intent Start ************************/ 

      // Define the file-name to save photo taken by Camera activity 

      String fileName = "Camera_Example.jpg"; 

      // Create parameters for Intent with filename 

      ContentValues values = new ContentValues(); 

      values.put(MediaStore.Images.Media.TITLE, fileName); 

      values.put(MediaStore.Images.Media.DESCRIPTION, "Image capture by camera"); 

      // imageUri is the current activity attribute, define and save it for later usage 

      imageUri = getContentResolver().insert(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 

      /**** EXTERNAL_CONTENT_URI : style URI for the "primary" external storage volume. ****/ 


      // Standard Intent action that can be sent to have the camera 
      // application capture an image and return it. 

      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

      intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 

      intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); 

      startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 

      /*************************** Camera Intent End ************************/ 


     } 

onActivityResult方法是在這裏:

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

     if (resultCode == RESULT_OK) { 

      /*********** Load Captured Image And Data Start ****************/ 

      String imageId = convertImageUriToFile(imageUri, CameraActivity); 


      // Create and excecute AsyncTask to load capture image 

      new LoadImagesFromSDCard().execute("" + imageId); 

      /*********** Load Captured Image And Data End ****************/ 


     } else if (resultCode == RESULT_CANCELED) { 

      Toast.makeText(this, " Picture was not taken ", Toast.LENGTH_SHORT).show(); 
     } else { 

      Toast.makeText(this, " Picture was not taken ", Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 
+1

請給我你的onActivityResult()代碼 –

+0

我用你的代碼,並有一個例外,java.lang.SecurityException異常:權限拒絕:寫com.android.providers.media.MediaProvider uri content:// media/external/images/media from pid = 6550,uid = 10103需要android.permission.WRITE_EXTERNAL_STORAGE或grantUriPermission() –

+0

你沒有在清單文件中添加權限。 curiousMind

0

//不使用data.getData(),因爲它在某些設備返回null。

所以使用光標獲取拍攝的圖像。

@Override 
protected void onActivityResult(int requestCode, int resultCode, 
    Intent resultData) { 
super.onActivityResult(requestCode, resultCode, resultData); 

    if (resultData != null) { 

    String[] projection = { MediaStore.Images.Media.DATA }; 
      Cursor cursor = managedQuery(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
        projection, null, null, null); 
      int column_index_data = cursor 
        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToLast(); 

      String imagePath = cursor.getString(column_index_data); 
      Bitmap bitmapImage = BitmapFactory.decodeFile(imagePath); 
      imageView.setImageBitmap(bitmapImage); 

     } 
相關問題