2012-10-24 65 views
2

我的应用程序涉及从设备的图库中选择图像,然后将该图像的较小版本保存到SD卡上的文件夹。我遇到的问题是有些用户报告说图像没有保存到文件夹中。然而,大多数用户报告说,该应用程序工作正常,我不能告诉这些其他几个用户正在发生什么。到目前为止,据报道这些问题的设备如下:华为T-Mobile myTouch,三星GT-S5830i,HTC Evo 4G和三星Galaxy S2。我自己有一个摩托罗拉Atrix 2,我没有这样的问题。将图像保存到所有设备上的SD卡

我的清单已经有标签了。我的大部分代码都来自其他的stackoverflow解决方案,从图库中获取图像,然后将其保存到SD卡。

从库获取图像:

public void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == RESULT_OK) 
    { 
     switch(requestCode) 
     { 
      case SELECT_IMAGE: 
       image_dir = getPath(data.getData()); 

       Bitmap myBitmap = decodeFile(new File(image_dir)); 

       resizedBitmap = Bitmap.createScaledBitmap(myBitmap, (int)(myBitmap.getWidth()/2), (int)(myBitmap.getHeight()/2), true); 
       break; 
     } 
    } 
    else 
    { 
     image_dir = "None"; 
    } 
} 

将图像保存到SD卡:

OutputStream fOut = null; 
File file = new File(Environment.getExternalStorageDirectory()+"/MyApp",imgname+".jpg"); 
fOut = new FileOutputStream(file); 

resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut); 
fOut.flush(); 
fOut.close(); 

而这一切似乎迎刃而解对于大多数用户,但对于一些用户的图像ISN没有得救。这可能是一个权限问题或某种我在代码本身忽略的设置?任何帮助表示赞赏。根据this

回答

0

This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened 

Environment.getExternalStorageDirectory()将返回null,所以你应该检查Environment.getExternalStorageState()第一。

0

原因可能是这些设备不使用流行的外部存储约定/ mnt/sdcard /。以下是Samsung Galaxy S2HTC Evo 4G的一些提示。问题可能不是SD没有安装,但你应该按照jaredzhang的建议定义运行该检查。 我也建议听最可能的错误并报告它们 - 例如,检查SD是否已安装,目标文件夹是否存在等等。不用说,最好是将手放在其中一台设备上并检查为自己。也许你也可以在线搜索这些设备的getExternalStorageDirectory()的结果是什么。