How does appear saved SD card images in Android's Gallery? - android

I save an image to the sdcard and it doesn't appear in the Phone's Gallery. I can see saved image in a folder, but it folder doesn't apper in gallery.
My codes here, how fix it?
img_icon.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
img_resim.buildDrawingCache();
Bitmap bm = img_resim.getDrawingCache();
OutputStream fOut = null;
Uri outputFileUri;
try {
root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "folder_name" + File.separator);
root.mkdirs();
File sdImageMainDirectory = new File(root, "myPicName.jpg");
outputFileUri = Uri.fromFile(sdImageMainDirectory);
fOut = new FileOutputStream(sdImageMainDirectory);
} catch (Exception e) {
Toast.makeText(context.getActivity(),
"Error occured. Please try again later.",
Toast.LENGTH_SHORT).show();
}
try {
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
}
context.getActivity()
.sendBroadcast(
new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse("file://"
+ Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES))));
return true;
}
});
return rowView;
}
Thanks for helping..

Your problem lies here:
context.getActivity()
.sendBroadcast(
new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse("file://"
+ Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES))));
You are asking Android to re-index all of the files in Environment.DIRECTORY_PICTURES.
Asking to scan a whole directory tree is wasteful. In your case, it it even more wasteful, since you are not writing your file to that directory. Instead, you are writing that file to:
root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "folder_name" + File.separator);
root.mkdirs();
File sdImageMainDirectory = new File(root, "myPicName.jpg");
Hence, your scan will not pick up this file, which you are writing to some random spot on external storage.
You need to decide the proper place to store your file, then index that one file.

Related

how to save multiple bitmaps fastly in android studio

I want to save bitmaps in the gallery.
Currently, I am using the following code:
public void saveBitmap(Bitmap output){
String filepath = Environment.getExternalStorageDirectory().toString() + "/Imverter/ImverterEffectedImage";
File dir = new File(filepath);
if(!dir.exists()){
dir.mkdir();
}
String fileName = "Imverter" + System.currentTimeMillis() + ".jpg";
File image = new File(dir, fileName);
try {
FileOutputStream fileOutputStream = new FileOutputStream(image);
output.compress(Bitmap.CompressFormat.JPEG, 80, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
It saves one single bitmap efficiently, but in my app, I have to deal with multiple bitmaps, and this method results in the slow output.
I want to store every single bitmap in a different files.
Thanks in advance.

android - add images from drawable folder into a created file directory

I'm looking to be able to take png images (called 1.png, 2.png, 3.png) that I have stored in my apps drawable folder and copy/save them to a file directory that is created by my app on launch.I've tried .createnewfile() but was unable to get anything to work.code for creating my file directory below:
//-----create file directory for images
public void createDirectory() {
try {
File imageDirectory = new File(Environment.getExternalStorageDirectory() + "/DCIM/C2AT_IMAGES");
if (!imageDirectory.exists()) {
imageDirectory.mkdirs();
//something here to take images from drawable folder and place them into my newely created directory
File imageFile = new File(Environment.getExternalStorageDirectory() + "/DCIM/C2AT_IMAGES", //something here getDrawable maybe?
);
imageFile.createNewFile();
}
}
catch (Exception e) {
Log.d("MAKE_DIR", "error creating directory");
}
}
thanks
Solved, this is how I done it:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.maptile_1);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
File f = new File(Environment.getExternalStorageDirectory() + "/DCIM/C2AT_IMAGES"
+ File.separator + "test.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
e.printStackTrace();
toastMsg = e.toString();
toast();
}

How to create a raw image file with content inside to emulate an SD card for Android

I'm working on app which will read folders and files from an SD card, now to need to create a raw image file which will emulate the SD card, but when I use mksdcard it only creates an empty raew image file.
void saveImage() {
File filename;
try {
String path = Environment.getExternalStorageDirectory().toString();
new File(path + "/folder/subfolder").mkdirs();
filename = new File(path + "/folder/subfolder/image.jpg");
FileOutputStream out = new FileOutputStream(filename);
bitMapImg.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
MediaStore.Images.Media.insertImage(getContentResolver(), filename.getAbsolutePath(), filename.getName(), filename.getName());
Toast.makeText(getApplicationContext(), "File is Saved in " + filename, 1000).show();
} catch (Exception e) {
e.printStackTrace();
}
}

disallow images to display on gallery

I am have downloaded some images into my sd card and i would like to NOT display them in my gallery. Is there a way to remove them? I have read about .nomedia file but how to do I create them?
is it by doing this?
File storagePath = new File(Environment.getExternalStorageDirectory(),folderName+"/Covers/");
if (!storagePath.exists())
{
File file = new File(storagePath, ".nomedia");
if (!file.exists()) {
try {
file.createNewFile();
Log.d("created","successful");
}
catch(IOException e) {
}
}
}
Here's a snippet that will do what you want:
File storagePath = new File(Environment.getExternalStorageDirectory(),
folderName + "/Covers");
storagePath.mkdirs();
File filename;
filename = new File(storagePath + "/" + ".nomedia");
if (!filename.exists()) {
filename.createNewFile();
}
Note: It might throw an IOException if it cannot create the file, you should catch this.
It will take likely a reboot for the mediascanner to rescan and determine it should not index that location.

Android Saving a Bitmap in a Folder -Duplicates-

My problem is whenever I save the bitmap. I saves one in the folder I want(MyFolder) and one in the DCIM/Camera. I don't see any code that makes it save int he DCIM directory?
Here are my codes...
case R.id.menu_save:
try {
String path = Environment.getExternalStorageDirectory()
.toString();
File myNewFolder = new File(path + "/MyFolder");
myNewFolder.mkdirs();
OutputStream fOut = null;
File file = new File(path, "/MyFolder/HK" + filename + ".jpg");
fOut = new FileOutputStream(file);
newBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(getContentResolver(),
file.getAbsolutePath(), file.getName(), file.getName());
Toast.makeText(getApplicationContext(),
filename + "Has been saved!", Toast.LENGTH_LONG)
.show();
} catch (Exception e) {
Toast.makeText(
getApplicationContext(),
"Problem to Save the File", Toast.LENGTH_LONG).show();
}
break;
As in the thread #Dixit linked, you can specify the file path URI with
File fileDir = new File(Environment.getExternalStorageDirectory() +
"/saved_images");
fileDir.mkdirs();
File file = new File(fileDir, "image.jpg");
Uri outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
This way the camera will save the image to the specified path instead of the DCIM folder.
EDIT: You have to create the folder on the sdcard beforehand, maybe that's the problem. Otherwise, this should work.

Categories

Resources