Java 如何重命名内部存储器中的文件?
我有一张从用户相册中获取的图像,并将其保存在一个文件夹中 代码如下:Java 如何重命名内部存储器中的文件?,java,android,filenotfoundexception,internal-storage,Java,Android,Filenotfoundexception,Internal Storage,我有一张从用户相册中获取的图像,并将其保存在一个文件夹中 代码如下: filename = "pippo.png"; try { ContextWrapper cw = new ContextWrapper(getApplicationContext()); File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
filename = "pippo.png";
try {
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File myPath = new File(directory,filename);
FileOutputStream out = new FileOutputStream(myPath);
theImage.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
Log.d("Image","saved success");
picture = directory.getAbsolutePath();
} catch (Exception e) {
e.printStackTrace();
Log.d("Image","saved failed");
}
try {
File f = new File(filePath, username+".png");
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
playerImage.setImageBitmap(b);
Log.d("Image","second load succcess"); }
catch (FileNotFoundException e)
{
e.printStackTrace();
Log.d("Image","second load failed"); }
然后,我读取图像并通过该代码更改其名称:
if(comingIntent.hasExtra("FILEPATH"))
{
filePath = comingIntent.getStringExtra("FILEPATH");
String filename = "pippo.png";
try {
File f = new File(filePath, filename);
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
playerImage.setImageBitmap(b);
File newfile = new File(filePath,username+".png");
f.renameTo(newfile);
Log.d("Image","first load succcess");
}
catch (FileNotFoundException e)
{
e.printStackTrace();
Log.d("Image","first load failed");
}
但是,当我尝试用新名称重新加载图像时,我得到一个文件未找到异常,这是代码:
filename = "pippo.png";
try {
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File myPath = new File(directory,filename);
FileOutputStream out = new FileOutputStream(myPath);
theImage.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
Log.d("Image","saved success");
picture = directory.getAbsolutePath();
} catch (Exception e) {
e.printStackTrace();
Log.d("Image","saved failed");
}
try {
File f = new File(filePath, username+".png");
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
playerImage.setImageBitmap(b);
Log.d("Image","second load succcess"); }
catch (FileNotFoundException e)
{
e.printStackTrace();
Log.d("Image","second load failed"); }
这就是日志错误:
08-09 20:23:50.730 15052-15052/?W/System.err:
java.io.FileNotFoundException:lol1.png:open失败:enoint(没有这样的
文件或目录)08-09 20:23:50.743 15052-15052/?W/系统错误:在libcore.io.IoBridge.open中(IoBridge.java:452) 位于java.io.FileInputStream。(FileInputStream.java:76) 位于com.example.abzo.socsoc.PlayerHomePage.onCreate(PlayerHomePage.java:103) 位于android.app.Activity.performCreate(Activity.java:6285) 位于android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2414)上 位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2521) 在android.app.ActivityThread.access$900(ActivityThread.java:150) 在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1383) 位于android.os.Handler.dispatchMessage(Handler.java:102) 位于android.os.Looper.loop(Looper.java:148) 位于android.app.ActivityThread.main(ActivityThread.java:5517) 位于java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run上(ZygoteInit.java:726) 位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 原因:android.system.ErrnoException:open失败:enoint(没有这样的文件或目录) 在libcore.io.Posix.open中(本机方法) 在libcore.io.BlockGuardOs.open上(BlockGuardOs.java:186) 在libcore.io.IoBridge.open中(IoBridge.java:438) ... 14更多08-09 20:23:50.744 15052-15052/?D/Image:第二次加载失败
正如我们在评论中所讨论的,我相信您的重命名()由于文件被某种类型的保留而无法工作,但是您没有发布完整的代码,因此我不能完全确定 使用您提供的代码,我创建了一个MainActivity,用于对存储在内部存储器中的文件进行重命名(即pippo.png)。运行“活动”时,调试日志会证明重命名成功 注意:在我下面的解决方案中,我只是创建文件并将它们放在您说应该放在的位置,以便为您提供如何在您的应用程序中使用renameTo()的答案,我实际上并没有处理图像,因为您没有向我提供用于访问图像的代码。我相信您已经意识到了这一点,但是您需要确保用户正确选择了您正在使用的图像,并且当您将示例插入到实际应用程序中时,其路径是准确的
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String imageFilename = "pippo.png";
//example username, I am not sure how you get this info
String exampleUsername = "user1";
try {
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
//check that we are good here...
if(directory.exists())
Log.d("ImageTAG", "'imageDir' exists");
// create imageDir
File completeImagePath = new File(directory, imageFilename);
//write file
FileOutputStream out = new FileOutputStream(completeImagePath);
out.flush();
out.close();
//check to ensure complete image path exists... it should
if(completeImagePath.exists())
Log.d("ImageTAG", "'completeImagePath' exists");
//show full path on device
Log.d("ImageTAG", "Image saved success, complete Image Path: " +
completeImagePath.getAbsolutePath());
//redeclaration of file here is not needed, but added for clarity
File from = new File(completeImagePath.getAbsolutePath());
//what you are renaming the file to
File to = new File(directory, exampleUsername + ".png");
//now rename
Boolean success = from.renameTo(to);
Log.d("ImageTAG", "Successful Rename: "+success.toString()+"| File is now named: "+to.getPath());
} catch (Exception e) {
e.printStackTrace();
Log.d("ImageTAG","saved failed");
}
}
}
您是否已确定该文件实际上已被内部存储器中的代码重命名?虽然查看您发布的代码时,这似乎不是您的问题,但我以前在进行编辑时遇到过renameTo()问题。你能提供日志吗?@ViaTech这是奇怪的部分,我创建的文件夹“imageDir”不在数据>数据>我的应用程序中,我找不到它,即使它从第一次加载时读取了图像..请确保一秒钟。@ViaTech这是路径/data/user/0/com.example.abzo.socsoc/app_imageDir,但如何访问它,当我使用Device explorerHmm时,在数据中看不到用户。。您上面提供的代码似乎不完整,因此我无法直接重新创建您的问题(至少目前是这样),但由于您得到的错误是没有这样的文件或目录,因此您试图打开一个不存在的文件。发生这种情况有几个原因,很容易检查您是否确实可以从资源管理器访问目录,但由于您没有访问权限,因此需要使用日志对路径进行故障排除。在第二个代码段中,在重命名和Log.d(“Image”,“first load success”)之后设置filePath=f.getAbsolutePath();这有什么作用吗?@ViaTech it没有重命名它,我检查了它..所以没有这样的文件,为什么它没有重命名它?