0

我正在尝试将我不久前拍摄的照片保存到另一个文件夹中(该文件夹已经存在)。我的清单在我需要的权限上是最新的,并且我知道我需要使用以下内容:

MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);  

以下是我的代码。我做错了什么,它不会保存?谢谢!

save.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

int id = viewIt.getId();

String root = Environment.getExternalStorageDirectory().toString();
String myDir = new String(root + "/New Directory/Folder 1/"); 
String description = "";
String filestring = arrPath[id];        
try {
    MediaStore.Images.Media.insertImage(getContentResolver(), myDir, filestring, description);
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } 
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
    Uri.parse("file://" +  Environment.getExternalStorageDirectory())));

    Toast.makeText(getApplicationContext(), "Save Successful", Toast.LENGTH_SHORT).show();          


    }
});

以下是我的 logcat 中的错误:

04-20 17:38:03.763: E/AndroidRuntime(1003): FATAL EXCEPTION: main
04-20 17:38:03.763: E/AndroidRuntime(1003): java.lang.NullPointerException
04-20 17:38:03.763: E/AndroidRuntime(1003):     at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:551)
04-20 17:38:03.763: E/AndroidRuntime(1003):     at com.test.Import$ImageAdapter$2$4$1.onClick(Import.java:339)
04-20 17:38:03.763: E/AndroidRuntime(1003):     at android.view.View.performClick(View.java:2485)
04-20 17:38:03.763: E/AndroidRuntime(1003):     at android.view.View$PerformClick.run(View.java:9080)
04-20 17:38:03.763: E/AndroidRuntime(1003):     at android.os.Handler.handleCallback(Handler.java:587)
04-20 17:38:03.763: E/AndroidRuntime(1003):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-20 17:38:03.763: E/AndroidRuntime(1003):     at android.os.Looper.loop(Looper.java:123)
04-20 17:38:03.763: E/AndroidRuntime(1003):     at android.app.ActivityThread.main(ActivityThread.java:3683)
04-20 17:38:03.763: E/AndroidRuntime(1003):     at java.lang.reflect.Method.invokeNative(Native Method)
04-20 17:38:03.763: E/AndroidRuntime(1003):     at java.lang.reflect.Method.invoke(Method.java:507)
04-20 17:38:03.763: E/AndroidRuntime(1003):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-20 17:38:03.763: E/AndroidRuntime(1003):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-20 17:38:03.763: E/AndroidRuntime(1003):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

0

1) 带空格的文件夹名称可能会出现问题,因此请确保检查文件夹是否存在,否则创建它。

File file = new File(myDirPath); 
if(!file.exists()) {
  if(!file.mkdirs()){
    Log.e("TAG", "Error: You can't create folder");   
  }
}

2) 可能有一个错误,但您没有看到它,因为您捕获了异常但不打印它。

尝试更换这个

e.printStackTrace();

Log.e("TAG", "错误:"+ e.toString());

于 2013-04-20T17:08:49.737 回答
-1

我想到了!我决定走一条不同的路线,只是重命名文件。我意识到我不想要多个相同的图像(试图节省 sdcard 空间)所以我这样做了:

int id = viewIt.getId();

File sdcard = Environment.getExternalStorageDirectory();
File sdddcard = new File(sdcard + "/New Directory/Folder 1/");

File from = new File(arrPath[id]);


Random generator = new Random(); 
int n = 10000;
n = generator.nextInt(n);
String filename = "ImageImport-"+ n +".jpg";

File to = new File(sdddcard,filename);

from.renameTo(to);
于 2013-04-22T19:08:10.663 回答