【发布时间】:2015-04-03 08:44:50
【问题描述】:
我已经苦苦挣扎了两天,试图了解在 Android 中将文件复制到 SD 卡的过程。到目前为止,我尝试过的所有方法似乎都不起作用。
我的应用程序具有个人资料图片设置。我需要启动一个 Intent 来选择图像,然后我需要将图像复制到 SD 卡上的新路径,然后返回新图像的 Uri,此时我检查图像方向(三星图片似乎被旋转有时90度)。然后我正确旋转图像,然后将 Uri 保存到 SharedPreferences 文件以在应用程序中使用。
这是我的意向电话:
case R.id.ib_userImage:
i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(i, 1);
break;
这是我目前对复制功能的可怕尝试,我已经改变了很多,我并没有很迷茫。
public static void copyImage(Context context, Uri uri) {
Log.i("ATTENTION", "Inside the Copy Function");
Log.i("ATTENTION", "Trying to copy file: " + uri.toString());
try {
String outputPath = Environment.getExternalStorageDirectory() + "/appname/images/";
File dir = new File(outputPath);
if(!dir.exists()) {
dir.mkdirs();
}
Log.i("ATTENTION", "Destination File Created at: " + dir.toURI().toString());
InputStream in = context.getContentResolver().openInputStream(uri);
OutputStream out = new FileOutputStream(dir);
byte[] buffer = new byte[1024];
while(in.read(buffer) > 0) {
out.write(buffer);
}
out.flush();
out.close();
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.i("ATTENTION", "File Copied");
}
感谢您的帮助,我会提供您可能需要的任何其他信息。
更新:
我现在在写入过程中遇到以下异常
java.io.FileNotFoundException: /storage/emulated/0/appname/images: open failed: EISDIR (Is a Directory);
我的理解是我使用以下代码指定了一个目录:
String outputPath = Environment.getExternalStorageDirectory() + "/medinfo/images/";
File dir = new File(outputPath);
if(!dir.exists()) {
dir.mkdirs();
}
然后将其传递给 OutputStream:
OutputStream out = new FileOutputStream(dir);
OutputStream 会在该目录中为我创建文件。
我不认为这实际上是在尝试打开目录。
【问题讨论】: