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

android tutorials screen
LocationManager.getLastKnownLocation returns null when it runs just after system booted
Using JSch with eclipse (Android project)
multicolour bar chat in android
Login flow for Gigya in mobile app with custom login UI
Converting a Drawable to a Bitmap to change the color of a Marker in Google Maps Android API v2
ActivityNotFoundException: Unable to find explicit activity class - in Jelly bean (4.2.2)
R cannot be resolved to a variable | Could not find [YourAndroiProjectName].apk
Android: achartEngine with fragments
Code suddenly crashes
Android back softkey how to use
updating notification progress failed why?
Twitter4j throws 'No authentication challenges found' after canceling login
Send android device to sleep mode
NullpointerException on string-array
How to pass values between Fragments

Categories

HOME
android
cygwin
xml
linkedin
telegram
dns
gpu
apiblueprint
rendering
pdfbox
entity-framework-core
frontend
code-generation
enterprise-architect
eip
screen-readers
modbus-tcp
glyphicons
google-content-api
html5-appcache
jersey-1.0
articulate-storyline
react-toolbox
string-concatenation
resourcebundle
onload
gpio
fable-f#
panoramas
aws-codecommit
event-flow
cognos-bi
deltaspike
arduino-ide
protege4
hivemq
imagenet
jpad
gdata
setup.py
apache-stanbol
linqpad
subclipse
clang++
reportservice2010
facebook-pixel
dbcontext
manova
insight
corruption
android-testing
universal-analytics
pocketsphinx-android
choice
change-tracking
file-uri
dimension
eigenvector
http-status-code-401
nio2
kendo-upload
blitline
intel-c++
ons-api
dock
dronekit-android
branch-and-bound
matlab-deployment
at-job
jbake
iiop
cbind
rmysql
gii
koala
xmltodict
gd-graph
jammer
kendo-dataviz
mbunit
inappsettingskit
web-notifications
android-loadermanager
onmousemove
adobe-media-server
cuda-gdb
node-redis
fsevents
drawtobitmap
uipagecontrol
event-receiver
inline-formset
django-paypal
codebase
luajava
ie-compatibility-mode
office-2007
web-application-project
complex-event-processing
asynchronous-wcf-call
flvplayback
java1.4

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