从Android上的图库功能加载 - 有时会失败,为什么?

时间:2013-01-27 11:47:16

标签: android android-intent android-activity android-gallery

我正在使用loadfromgallery功能从图库中加载和裁剪图片, 但有时它不起作用。

似乎不能在没有SDCard的设备上工作(我在各种设备上测试过,在我的i9000 + CM10.1 +信号量中使用和w / out总能正常工作,在平板电脑上使用android> 4.0不起作用)

有人能帮助我吗?

这是我的onActivityResult方法:

protected void onActivityResult(int requestCode, int resultCode,Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    switch (requestCode) {
    case REQ_CODE_PICK_IMAGE:
        if (resultCode == RESULT_OK) {  
            if (imageReturnedIntent!=null){
                getTempFile();
                String filePath= Environment.getExternalStorageDirectory()+ "/temp/"+TEMP_PHOTO_FILE;
                System.out.println("path "+filePath);
                Bitmap selectedImage =  BitmapFactory.decodeFile(filePath);
                bitmapdrawable = new BitmapDrawable(selectedImage);
                generator_imagen.setImageDrawable(bitmapdrawable);
                app_gestorVistas("GENERATE");
                app_posicionGrid=9999;
            }
        }
    }} 

这是功能:

public void loadfromgallery(){  //TODO LOAD FROM GALLERY
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    photoPickerIntent.setType("image/*");
    photoPickerIntent.putExtra("crop", "true");
    photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
    photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
    startActivityForResult(photoPickerIntent, REQ_CODE_PICK_IMAGE);
}
private Uri getTempUri() {
    return Uri.fromFile(getTempFile());
}

private File getTempFile() {
    if (isSDCARDMounted()) {
        File f = new File(Environment.getExternalStorageDirectory(),"/temp/"+TEMP_PHOTO_FILE);

        try {f.createNewFile();}
        catch (IOException e) {}
        return f;
    } else {
        return null;
    }
}

private boolean isSDCARDMounted(){
    String status = Environment.getExternalStorageState();
    if (status.equals(Environment.MEDIA_MOUNTED))
        return true;
    return false;
}



private static final String TEMP_PHOTO_FILE = "mprt_img.jpg";

这是我过滤的logcat(生命周期)

   01-27 13:34:29.476: V/EVENTO(4630): STATE:  onCreate
01-27 13:34:36.261: V/EVENTO(4630): STATE:  onStart
---APP BUSY--
01-27 13:34:47.296: V/EVENTO(4630): STATE:  onStop
--STOP APP AN DSHOW IMAGEPICKER---
01-27 13:35:01.316: V/EVENTO(4630): STATE:  onRestart
01-27 13:35:01.332: V/EVENTO(4630): STATE:  onStart

1 个答案:

答案 0 :(得分:0)

你可以发布你的logcat输出吗?

你的 TEMP_PHOTO_FILE 是什么?您的主要活动的生命周期是什么?当你从图像选择器回来时,它会再次调用onCreate吗?如果是这样,您将丢失所有临时变量,并且应将它们保存在捆绑包中。

    @Override
public void onSaveInstanceState(Bundle outState) {
  super.onSaveInstanceState(outState);
  if (filesaved == null) {
    outState.putString("file-uri", "");
  }
  else {
    outState.putString("file-uri", filesaved.toString());
  }
};

另一个问题可能是文件的创建。确保文件夹存在!

File sdCard = Environment.getExternalStorageDirectory();

File dir = new File (sdcard.getAbsolutePath() + "/temp/");

dir.mkdirs();

File file = new File(dir, TEMP_PHOTO_FILE);

...

FileOutputStream f = new FileOutputStream(file);