我正在尝试找到一种简单的方法来获取具有已知路径和文件夹的图像,并将其移动到库中的新文件夹。调用此方法时,通过从android Images MediaStore中查询的bucketNames列表中选择来提供bucketName。
我遇到的问题是try块永远不会被执行,无论bucketName最终是什么,因为当尝试启动OutputStream时,它会说找不到文件并跳转到catch块。但是,我在目的地创建一个文件,所以我不确定为什么会这样说。我一直在寻找替代解决方案,但是这里的几乎所有建议似乎都是使用这种方法,或者是TransferFrom或TransferTo(这也会产生同样的问题)。
编辑:我应该注意,每次打开应用程序时都要检查Write_External_Storage烫发。
public void moveImage(int position, String bucketname)
{
String oldPath = imgList.get(position).getImage_path();
String newPath = oldPath.replace(imgList.get(position).getImage_bucket(), bucketname);
Log.i("index", oldPath);
Log.i("index", newPath);
InputStream inputStream = null;
OutputStream outputStream = null;
try
{
File newFile = new File(newPath);
if (!newFile.exists())
{
boolean result = newFile.mkdirs();
Log.i("index", "file didnt exist");
}
Log.i("index", "file exists");
inputStream = new FileInputStream(new File(oldPath));
outputStream = new FileOutputStream(newFile);
byte[] buffer = new byte[1024];
int read;
while ((read = inputStream.read(buffer)) != -1)
{
outputStream.write(buffer, 0, read);
}
inputStream.close();
inputStream = null;
// write the output file (You have now copied the file)
outputStream.flush();
outputStream.close();
outputStream = null;
Log.i("index", "successful");
}
catch (IOException ex)
{
ex.printStackTrace();
Log.i("index", "unsuccessful");
}
//String selection = MediaStore.Images.Media.DATA + "=?";
//String[] selectionArgs = {String.valueOf(imgList.get(position).getImage_path())};
//ctx.getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection, selectionArgs);
imgList.remove(position);
//Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
//intent.setData(Uri.fromFile(newFile));
//ctx.sendBroadcast(intent);
}
这是我正在获取的堆栈跟踪 - 据说存在newFile,打印文件的旧路径和新路径
I/index: /storage/emulated/0/Download/retrowave_80_s_bg_by_rafael_de_jongh-d9wsq5j.png
I/index: /storage/emulated/0/Camera/retrowave_80_s_bg_by_rafael_de_jongh-d9wsq5j.png
I/index: file exists
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Camera/retrowave_80_s_bg_by_rafael_de_jongh-d9wsq5j.png (Is a directory)
W/System.err: at java.io.FileOutputStream.open(Native Method)
W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
W/System.err: at com.example.kyler.bugout.MediaServer.moveImage(MediaServer.java:255)
W/System.err: at com.example.kyler.bugout.MainActivity$4.onBucketClick(MainActivity.java:383)
W/System.err: at com.example.kyler.bugout.MoveImageDialogFragment$1.onClick(MoveImageDialogFragment.java:44)
W/System.err: at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:1119)
W/System.err: at android.widget.AdapterView.performItemClick(AdapterView.java:310)
W/System.err: at android.widget.AbsListView.performItemClick(AbsListView.java:1155)
W/System.err: at android.widget.AbsListView$PerformClick.run(AbsListView.java:3126)
W/System.err: at android.widget.AbsListView$3.run(AbsListView.java:4041)
W/System.err: at android.os.Handler.handleCallback(Handler.java:751)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at android.os.Looper.loop(Looper.java:154)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6077)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
I/index: unsuccessful
答案 0 :(得分:0)
if (!newFile.exists())
{
boolean result = newFile.mkdirs();
Log.i("index", "file didnt exist");
}
这将创建一个目录retrowave_80_s_bg_by_rafael_de_jongh-d9wsq5j.png
(在您的例外中实际提到)。您应该将块更改为
if (!newFile.exists())
{
boolean result = newFile.getParentFile().mkdirs();
Log.i("index", "file didnt exist");
}