2011-06-26 68 views
1

我正在使用這兩個類來請求並從圖庫中檢索圖片。此代碼在薑餅和下面的工作良好,但在Xoom的Honeycomb中失敗。Android Gallery 3D不會返回蜂窩中的位圖

我看到它的行爲它寫了一個空白文件,但畫廊沒有將選定的圖片寫入該文件。此外,該文件在Windows中不可見,我必須轉到DDMS選項卡才能看到要創建的文件。它擁有所有者和團體的訪問權限,但不是每個人。

改編自:Retrieve Picasa Image for Upload from Gallery

public static class GetImage implements IIntentBuilderForResult { 
    public String TypeFilter = "image/*"; 
    public boolean ForceDefaultHandlers = false; 
    public Bitmap.CompressFormat CompressFormat = Bitmap.CompressFormat.PNG; 

    private Intent intent = null; 
    private String TemporaryImagePath = null; 

    public void prepareIntent(Context context) { 
     try { 
      File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
      dir.mkdirs(); 
      File tempFile = new File(dir, "galleryresult-temp.png"); 

      //.createTempFile("GalleryResult", ".png"); 
      TemporaryImagePath = tempFile.getAbsolutePath(); 

      tempFile.getParentFile().mkdirs(); 

      tempFile.createNewFile(); 
      Logger.d("IsFile= " + tempFile.isFile()); 
      tempFile.setWritable(true, false); 
      tempFile.setReadable(true, false); 

      intent = new Intent(Intent.ACTION_GET_CONTENT); 
      intent.setType(TypeFilter); 

      if (ForceDefaultHandlers) { 
       intent.addCategory(Intent.CATEGORY_DEFAULT); 
      } 

      final Uri uri = Uri.fromFile(tempFile); 
      intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); 
      final String formatName = CompressFormat.name(); 
      intent.putExtra("outputFormat", formatName); 
     } catch (Exception e) { 

     } 
    } 

    public Intent getIntent() { 
     return intent; 
    } 

    public Bundle getResultBundle() { 
     Bundle data = new Bundle(); 
     data.putString("transientImagePath", TemporaryImagePath); 
     return data; 
    } 
} 

public abstract static class GetImageResult extends ActivityResultHandlerHelper { 

    public Bitmap bitmapResult = null; 

    public void onPrepareResult() { 
     bitmapResult = null; 
     Uri imageUri = null; 
     String filePath = null; 
     boolean fromTransientPath = false; 
     String tempFilePath = null; 

     if(resultBundle != null) { 
      tempFilePath = resultBundle.getString("transientImagePath"); 

      File tempFile = new File(tempFilePath); 
      imageUri = Uri.fromFile(tempFile); 
     } 
     if(imageUri == null || imageUri.toString().length() == 0) { 
      imageUri = data.getData(); 
     } else { 
      fromTransientPath = true; 
     } 

     if(imageUri != null) { 

      if(imageUri.getScheme().equals("file")) { 
       filePath = imageUri.getPath(); 
      } else if(imageUri.getScheme().equals("content")) { 
       filePath = findPictureFilePath(context, imageUri); 
      } 

      if(filePath != null) { 
       bitmapResult = BitmapFactory.decodeFile(filePath); 
       if(fromTransientPath) { 
        //File delTarget = new File(filePath); 
        //delTarget.delete(); 
       } 
      } 
     } 
    } 
} 

public static final String findPictureFilePath(Context context, Uri dataUri) { 
    String filePath = null; 
    final String[] projection = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = null; 
    try { 
     cursor = context.getContentResolver().query(dataUri, projection, 
       null, null, null); 
     int data_index = cursor 
       .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     if (cursor.moveToFirst()) { 
      filePath = cursor.getString(data_index); 
     } 
    } finally { 
     if (cursor != null) { 
      cursor.close(); 
     } 
    } 

    return filePath; 
} 

回答

0

啓動意圖得到的照片。

final Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
intent.setType("image/*"); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 
activity.startActivityForResult(intent, requestCode); 

接受返還的照片。

final InputStream is = context.getContentResolver().openInputStream(intent.getData()); 
final Bitmap imageData = BitmapFactory.decodeStream(is, null, options); 
is.close(); 

這個問題是如此令人發狂,我寫了一篇關於如何正確完成它的整篇文章。或者至少是最好的方式。

http://androidfragments.blogspot.com/2012/02/loading-bitmaps-from-gallery.html