I want to Store Image from Drawable folder to SDCard in Android.I tried a lot but I could not find the solution.Please Someone help me for my this issue.Thank you.
These images in drawable folder can be accessed by BitmapFactory, you can save the bitmap to PNG or JPG.
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
File sd = Environment.getExternalStorageDirectory();
String fileName = "test.png";
File dest = new File(sd, fileName);
try {
FileOutputStream out;
out = new FileOutputStream(dest);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
For other type of images, I think put them into assets folder is a better way.
Related
This is my code inside onActivityResult. PathUtil class is getting full file path from Uri. I am picking single picture from Gallery :)
try {
selectedFilePath = PathUtil.getPath(SmoothPhotoActivity.this, selectedImageUri);
File sd = Environment.getExternalStorageDirectory();
File image = new File(selectedFilePath);
String filePath = image.getPath();
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
Bitmap outputImage = imageFilter.processFilter(bitmap);
ivPhotoSelected.setImageBitmap(outputImage);
} catch (URISyntaxException e) {
e.printStackTrace();
}
I have to save a bitmap in a particular folder. If the folder is not created, I want to create it.
I know you can just save an image like that:
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap,
bitmap + ".jpg Card Image", bitmap + ".jpg Card Image");
But how can I save it in a specific location like "MediaStore/folder"(Don't actually know how the path should be) where folder is a folder that I want to create (if it's not created).
I've tried a lot of things and they didn't work so I would be very thankful if someone could help me!
You can use internal storage like so if you're willing to not use MediaStore
void saveBitmap(Bitmap bitmap) {
File dir = getFilesDir(); // or getCacheDir()
if (!new File(dir + "/folderName").isDirectory()) {
if (new File(dir + "/folderName").mkdir()) { // directory made successfully
dir = new File(dir + "/folderName/" + "fileName.ext");
} else { // error making the directory
return;
}
} else {
dir = new File(dir + "/folderName/" + "fileName.ext");
}
try {
FileOutputStream fileOutputStream = new FileOutputStream(dir);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
To retrieve the bitmap
Bitmap loadBitmap(File dir) {
Bitmap bitmap = null;
try {
FileInputStream fileInputStream = new FileInputStream(dir);
bitmap = BitmapFactory.decodeStream(fileInputStream);
fileInputStream.close();
} catch(Exception e) {
e.printStackTrace();
}
return bitmap;
}
When I take an image by Camera, its works fine and I get the image path to save in SQLite database and can retrieve the path to show required image. But when I select an image from Gallery I don't get the correct path which I need to save in database for the further retrieve. I don't get image cause the path is incorrect when I save path in the database. So my question is - how can I get the correct absolute path of the gallery image?
#RequiresApi(api = Build.VERSION_CODES.N)
private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bm = BitmapFactory.decodeFile(destination.getAbsolutePath());
ivImage.setImageBitmap(bm);
showImagepathET.setText(destination.getAbsolutePath());// This path will save in database
}
#SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
Bitmap bm=null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
File destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
showImagepathET.setText(destination.getAbsolutePath());// This path will save in database
ivImage.setImageBitmap(bm);
}
Here, I get the correct image to show in ImageView ivImage in both take an image by camera or gallery. But I don't get the path correctly when I pick an image from Gallery(works fine when I take camera Image) which I save in database for further path retrieve.
how i can get this selected image path
For what feels like the 3,735th time, there is no "selected image path". You are getting a Uri back (data.getData()); a Uri does not have to point to a file. For example, http://stackoverflow.com/questions/44057912/not-getting-correct-path-when-select-image-from-gallery is a Uri; it does not represent a file on the filesystem.
If you want a file, use a file-picker library.
At last, I decided to do the same procedure what I did in Method "onCaptureImageResult(Intent data)". The image I get from the Gallery stored again to a directory.I know this is absolutely not the good procedure, but it solved my problem temporally until I get a good answer.
#SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
Bitmap bm=null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bm1 = BitmapFactory.decodeFile(destination.getAbsolutePath());
ivImage.setImageBitmap(bm1);
String selectedImageUri = data.getData().getPath();
showImagepathET.setText(destination.getAbsolutePath());
}
How to copy image in Clipboard from drawable folder in android ?
I mean copy image from drawable folder in Clipboard and paste anywhere.
Plese Help me.....
if you want to copy the drawable image to sdcard you the below code.
You can do something like this,
if (isSdPresent()) { // to check is sdcard mounted
BitmapFactory.Options bmOptions;
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
Bitmap bbicon = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
String extStorageDirectory = Environment.getExternalStorageDirectory()+ File.separator + "FolderName";
File wallpaperDirectory = new File(extStorageDirectory);
wallpaperDirectory.mkdirs();
OutputStream outStream = null;
File file = new File(wallpaperDirectory,"icon.png");
//to get resource name getResources().getResourceEntryName(R.drawable.icon);
try {
outStream = new FileOutputStream(file);
bbicon.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
}
to check SDCard
public boolean isSdPresent() {
return android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
}
You cannot do it. Simply because Android clipboard doesn't support this link.
The only things that you can keep in clipboard are: text, uri and intent.
As you can see by the code below, I save the bitmap to a public picture directory. I have also provided a screenshot of the images in storage, as further proof that the images are being saved.
The problem is that the images do not show up in the Photo app, until I reboot my phone. I have checked other apps, and those pictures show up immediately. If I open the image, I also cannot edit it (e.g. add effects) like I can other pictures.
Bitmap b = Bitmap.createScaledBitmap(mBitmap, width, height, false);
Long uuid = UUID.randomUUID().getMostSignificantBits();
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES + "/test");
if(!path.exists() && !path.isDirectory()){
path.mkdirs();
}
Log.i("Directory:", path.toString());
File file = new File(path, "test_" + uuid + ".jpg");
if (file.exists()){
file.delete();
}
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
b.compress(Bitmap.CompressFormat.JPEG, 80, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
You must send a notification when adding a new image
getContentResolver().notifyChange(
Uri.parse("file://sdcard/abc.png");