0

我正在使用一个loadfromgallery函数从图库中加载和裁剪图像,但有时它不起作用。

它似乎不适用于没有 SDCard 的设备(我在各种设备上对其进行了测试,在我的 i9000+CM10.1+semaphore 和 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
4

1 回答 1

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);
于 2013-01-27T12:00:29.723 回答