notify image moved to another folder - android

I am a beginner android programmer. I create a project for hiding image. But, I have a problem in my project. Is that:
I use a method to move a photo from the folder A to folder .B(Here is how I hid it from image gallery) . I am sure that the picture in the folder A was deleted and moved to folder .B. However, when I open image gallery application I still see this picture is displayed at the folder A.
This is method copy picture to folder .B:
public static String copyFile(String path) {
//TO DO: create folder .B
File pathFrom = new File(path);
File pathTo = new File(Environment.getExternalStorageDirectory() + "/.B");
File file = new File(pathTo, fileToName);
while (file.exists()) {
fileToName = String.valueOf(System.currentTimeMillis());
file = new File(pathTo, fileToName);
}
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(pathFrom);
out = new FileOutputStream(file);
byte[] data = new byte[in.available()];
in.read(data);
out.write(data);
in.close();
out.close();
return file.getPath();
} catch (FileNotFoundException e) {
Log.e(TAG, e.getMessage());
return "error:" + e.getMessage();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
return "error:" + e.getMessage();
}
}
After copy picture to folder .B, I delete this picture in folder A:
new File(path).delete();
So Is there any suggestion for notify for all image gallery know that this picture was moved to another folder or another URI?
**UPDATE: The suggestion for me work fine is:
Before 4.4,you can call this:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://"+Environment.getExternalStorageDirectory())));
After 4.4,try this:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file)));
//the file is new image's path
THank FireSun and everyonce

After change imgae path,you should notify the gallery to update,so you should send a broadcast to make it.
Before 4.4,you can call this:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://"+Environment.getExternalStorageDirectory())));
After 4.4,try this:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file)));
//the file is new image's path

Why don't you use
pathFrom.delete();

Related

How to create folder in MIUI 10 programmatically?

I'm doing a module which is get images from server and save in internal storage of phone, Image fetching part in done but when i click on save button in some phones images store successfully but not store in MIUI operating system.
//Method for Save Image in Directory
private void saveSessionImage(int position, Bitmap bitmap, String se_photo_id_pk) {
File file;
String path = Environment.getExternalStorageDirectory().toString();
File myDir = new File(path + "/FolderName");
if (!myDir.exists()) {
myDir.mkdirs();
}
file = new File(myDir, "IMG_SESSION"+se_photo_id_pk+".jpg");
if (file.exists ()) {
file.delete ();
}
try{
OutputStream stream;
stream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);
stream.flush();
stream.close();
}
catch (IOException e) // Catch the exception
{
e.printStackTrace();
}
// Display saved image uri to TextView
Toast.makeText(context, "Saved in Gallery!", Toast.LENGTH_SHORT).show();
itemSessionPhotosList.remove(position);
notifyDataSetChanged();
}
i got "Saved in Gallery!" toast but inside storage there is no folder as well as image file
Log the path of external storage directory and check in the respective folder.

Android: open failed: ENOENT No such file or directory

I know have many question like my question. But It is different. I copy file from folder A to folder B in EXTERNAL_STORAGE use mothod below:
public static String copyFile(String path) {
String fileToName = String.valueOf(System.currentTimeMillis());
File pathFrom = new File(path);
File pathTo = new File(Environment.getExternalStorageDirectory() + "/.noname");
File file = new File(pathTo, fileToName + ".bak");
while (file.exists()) {
fileToName = String.valueOf(System.currentTimeMillis());
file = new File(pathTo, fileToName + ".bak");
}
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(pathFrom);
out = new FileOutputStream(file);
byte[] data = new byte[in.available()];
in.read(data);
out.write(data);
in.close();
out.close();
} catch (FileNotFoundException e) {
Log.e(TAG, e.getMessage());
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
return file.getPath();
}
The path param is: "/storage/emulated/0/Download/image_preview.jpg".
When execute this method I got an error: /storage/emulated/0/Download/tree_leaves_sunlight.jpg: open failed: ENOENT (No such file or directory).
Folder .noname have exists.
Is there any suggestion for my problem?
**UPDATE: This file I opening with ImageView. When I not open I can copy. But When I opening I got this error.
PS: I preview the image inImageView. And there have a Button copy image. When click to Button execute method copy this image to other folder.
When you create the File object for the parent directory
File pathTo = new File(Environment.getExternalStorageDirectory() + "/.noname")
Don't forget to actually create this folder
pathTo.mkdirs();
Also try to open file you're trying to copy in the gallery. It can be damaged and Android just can't open it.

Android: problems creating a file

I'm writing an Android application in which I want to create text files in a particular folder and afterwards I want to read the files from my device.
I'm doing this way:
File sd = Environment.getExternalStorageDirectory();
File f;
FileWriter fw = null;
String path = sd.getAbsolutePath() + "/Samples/";
f = new File(path+File.separator+"filename.txt");
if (!f.exists())
{
f.mkdirs();//Creates the directory named by this file, creating missing parent directories if necessary
try
{
f.createNewFile();
//fw = new FileWriter(f, true);
}
catch (IOException e)
{
Log.e("ERROR","Exception while creating file:"+e.toString());
}
The problem is that in this way I create another folder instead of a text file. What can I do? Thanks
Instead of:
f.mkdirs();
do:
path.mkdirs();
I found the solution and I want to share it with you:
File sd = Environment.getExternalStorageDirectory();
File folder;
String path = sd.getAbsolutePath() ;
folder = new File(path, dirName);
if (!folder.exists()){
folder.mkdirs();}
try{
File file = new File(folder, fileName+".txt");
file.createNewFile();
} catch (IOException e) {
Log.e("ERROR", "Exception while creating file:" + e.toString());
}
I hope this could help other people having the same problem. Good luck

Save / Load image to/from local storage

First of all, my permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
My methods to save and get the image:
private void saveIamgeToLocalStore(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/temp");
myDir.mkdirs();
String fname = "Profile_Image.png";
File file = new File (myDir, fname);
if (file.exists()) file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void loadImageFromLocalStore(String imageURI) {
try {
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().toString() + imageURI);
Bitmap bitmap = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri));
profileImage.setImageBitmap(bitmap);
profileImage.setTag("Other");
select_image_button.setText(R.string.button_remove_profile_picture);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Usage:
saveIamgeToLocalStore(BitmapFactory.decodeFile(picturePath));
loadImageFromLocalStore("/temp/Profile_Image.png");
I'm getting a
java.io.FileNotFoundException: No content provider: ...
warning.
What am I missing?
PS: The image gets saved in /mnt/sdcard/temp/ . The warning appears when loading the image.
Is your file getting saved? In case yes, may be the mediascanner is not triggered before you do a read of the file. Since the mediascanner is not triggered, so content provider wont have the entry
for your file (your file is not indexed).In case your file is getting saved with "saveIamgeToLocalStore", then trigger mediascanner from code once like this:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
.parse("file://"
+ Environment.getExternalStorageDirectory())));
and then do a read on the file. It should work.
You need to write file existing check. Then it'll be easier to find problem.
Short example
File test = new File (URI)
if ( test.exists() )
{
// do your computation
} else
{
// find problem in file path
}

store an image in internal storage in android

I am working now on application that start the camera and take a photo, I didn't use camera activity, but I wrote my camera app and I want to save the taken image in the internal phone storage in folder called "temPic"
The following code generate the folder and the image, but when I checked the tempPic I found an image called image1.jpg and it's size is 461115 ( I tried to store the image in SDcard directory and it is the same size), but when I double clicked it a black image appeared, not the taken one although in SDcard I opened it !!!
FileManipulator fileFormat = new FileManipulator(
getApplicationContext());
String path = fileFormat.createFolder_PStrg("tempPic") + "/image1.jpg";
File file = new File(path);
Uri outputFileUri = Uri.fromFile(file);
OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(outputFileUri);
imageFileOS.write(arg0);
imageFileOS.flush();
imageFileOS.close();
Toast.makeText(AndroidCamera.this,
file.length()+"",
Toast.LENGTH_LONG).show();
Toast.makeText(AndroidCamera.this,
"Image saved: " + outputFileUri.toString(),
Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
camera.startPreview();
I don't know if this can help you but this is how I save a file to the SD card and it works.
public void saveToSD(Bitmap outputImage){
File storagePath = new File(Environment.getExternalStorageDirectory() + "/MyPhotos/");
storagePath.mkdirs();
File myImage = new File(storagePath, Long.toString(System.currentTimeMillis()) + ".jpg");
try {
FileOutputStream out = new FileOutputStream(myImage);
outputImage.compress(Bitmap.CompressFormat.JPEG, 80, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Why don't you use the camera activity and save images to internal storage by creating temp folder that has writable permission which allow other application to write to this folder and take the image to its path and then after the camera activity return you can move the image to another internal storage folder but with private permission as follow
//the temp folder to start the camera activity with
String path = getDir("images", Context.MODE_WORLD_WRITEABLE).getPath() + "/test.jpg";
//start the camera activity
File file = new File(path);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, CAMERA_ACTIVITY);
and in onActivityResult method move it to folder with private permission it will work fine as I tested it

Categories

Resources