android


Save Image from res/drawable to Image Gallery Android


I want to Save Image from res/drawable to Image Gallery. I am using following code but it is doing nothing.
What is the wrong with my code ? String Drawable stands for Image Name which is there in drawable folder.
File direct = new File(Environment.getExternalStorageDirectory()
+ "/Images");
if (!direct.exists()) {
direct.mkdirs();
}
ByteArrayOutputStream bos = null;
FileOutputStream fos = null;
try {
Bitmap bitmap = BitmapFactory.decodeResource(
context.getResources(),
context.getResources().getIdentifier(
"#drawable/" + Drawable, "drawable",
context.getPackageName()));
bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 95, bos);
byte[] bitmapdata = bos.toByteArray();
fos = new FileOutputStream(direct + "/" + "IMG-" + CurrentDateTime
+ ".jpg");
fos.write(bitmapdata);
} catch (Exception e) {
Log.e("Internal Image Save Error->", e.toString());
} finally {
try {
if (bos != null) {
bos.close();
}
if (fos != null) {
fos.close();
fos.flush();
}
} catch (IOException ignored) {
Log.e("Internal Image Save Error->", ignored.toString());
}
}
I just found that it is saving image but It is taking some time like 10 mins.
Copy Image from Drawable to Gallery : It is giving File Not Found Exception on Input Stream. String Drawable is image name, i.e. data1
public static void downloadInternalImage(String Drawable, Context context) {
Toast.makeText(context, "Downloading Image...\nPlease Wait.",
Toast.LENGTH_LONG).show();
File direct = new File(Environment.getExternalStorageDirectory()
+ "/Images");
if (!direct.exists()) {
direct.mkdirs();
}
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream("android.resource://"
+ context.getPackageName() + "/drawable/" + Drawable + "");
output = new FileOutputStream(direct + "/" + "IMG-"
+ CurrentDateTime + ".jpg");
byte[] buf = new byte[1024];
int len;
while ((len = input.read(buf)) > 0) {
output.write(buf, 0, len);
}
Toast.makeText(context, "Image Saved.", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e("Internal Image Save Error->", e.toString());
Toast.makeText(context,
"Couldn't Save Image.\nError:" + e.toString() + "",
Toast.LENGTH_LONG).show();
} finally {
try {
if (input != null) {
input.close();
}
if (output != null) {
output.close();
}
} catch (IOException ignored) {
Log.e("Internal Image Save Error->", ignored.toString());
Toast.makeText(
context,
"Couldn't Save Image.\nError:" + ignored.toString()
+ "", Toast.LENGTH_LONG).show();
}
}
}

Related Links

Bluetooth low energy system design, response protocol
Why does Localytics require google-play-services:ads?
Android Webview tel: link ERR_UNKNOWN_URL_SCHEME
Error occur when i add jar file in android Studio
Google Play fails to connect, statusCode=RESOLUTION_REQUIRED
Get ImageView id inside multiple LinearLayout
Android Studio Error..Works on some devices and not others
Getting “java.lang.RuntimeException: here” while starting application
Vungle video ad is not showing but audio is playing
VolleyError: java.lang.IllegalArgumentException: timeout < 0
Version Control from Android App
cannot install two flavors of an app with Android Annotations
Calculating dates from given two dates excluding weekend in android
When ListView is ready?
using java.awt classes in android
Switch to a nested PreferenceScreen from activity

Categories

HOME
cheerio
telegram
alexa-skills-kit
static
battery
composite-primary-key
jsf-2.2
seo
babeljs
pearson-correlation
data-visualization
recordrtc
enthought
gson
x-cart
symmetricds
enterprise-architect
screen-readers
entitymanager
esoteric-languages
android-7.0-nougat
digital-signature
identity-management
sse
parcelable
pimcore
pchart
perlbrew
definitelytyped
ceylon
browser-sync
dst
cart
memory-address
mailkit
baidu-map
imx6
graphhopper
release
postgresql-8.4
android-checkbox
show-hide
flask-login
monkey-testing
servermanager
nanoc
ovf
textsum
tf
datamaps
corruption
truezip
reshape
swingworker
rocks
atlas
autosar
reporting-services-2012
actor-platform
lrs
mongodb-php
avspeechsynthesizer
android-wake-lock
riot
boost-serialization
exim4
oracle-spatial
forwarding
ons-api
video-player
sha512
keyup
nsurlsessiondatatask
rapidsvn
showdialog
webmatrix-3
jbake
dct
draw2d-js
square-bracket
git-repo
mbunit
patricia-trie
os.system
garbage
apache-whirr
koken
jsr286
entity-attribute-value
google-floodlight
w3c-geolocation
django-paypal
ajax-polling
blender-2.49
duplex
fail-fast-fail-early

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App