I have an image that I got from the user's album and I saved it in a folder.
here is the code:
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");
}
Then i read the image and change its name by that code:
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");
}
But then when I try to reload the image with its new name, I get a file not found exception, that's the code:
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"); }
That's the Log error:
08-09 20:23:50.730 15052-15052/? W/System.err: java.io.FileNotFoundException: lol1.png: open failed: ENOENT (No such file or directory) 08-09 20:23:50.743 15052-15052/? W/System.err:
at libcore.io.IoBridge.open(IoBridge.java:452) at java.io.FileInputStream.(FileInputStream.java:76) at com.example.abzo.socsoc.PlayerHomePage.onCreate(PlayerHomePage.java:103) at android.app.Activity.performCreate(Activity.java:6285) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2414) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2521) at android.app.ActivityThread.access$900(ActivityThread.java:150) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1383) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5517) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory) at libcore.io.Posix.open(Native Method) at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186) at libcore.io.IoBridge.open(IoBridge.java:438) ... 14 more 08-09 20:23:50.744 15052-15052/? D/Image: second load failed