I have a bitmap that I want to save to the sd card. I have 2 problems with that:
Set a name to the saved file
Save the file
I looked around Stack Overflow and couldn't find anything that worked, for both problems (mostly for the saving part, the set name part just got me all confused)
Is there a simple straight forward solution to this?
Thanks!
Bitmap yourBitmap; // you have to get your bitmap into this variable
String filePath = "/mnt/sdcard/"; // some times it may be only /sdcard not /mnt/sdcard
filePath += "newFileName.jpg";
try {
yourBitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File(filePath)));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You have to use below permission in manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Try this:
FileOutputStream out = new FileOutputStream(new File("/mnt/sdcard/pic.jpg"));
yourBitmap.compress(Bitmap.CompressFormat.JPG, 80, out);
out.close();
Related
I'm looking for how to store image data into my app on Android Wear.
What I want to do are followings:
Take a photo and send it to my watch. (via DataMap)
My watch displays the photo.
When my app on Android Wear restarts, the app displays the photo taken before.
For now, the photo is cleared after restart the app. I want to store the photo.
Are there any ways to save the photo into the watch.
Thanks.
[UPDATE1]
I tried to save an image by using Environment.getExternalStorageDirectory()
But "NOT EXISTS" is returned.
String imagePath = Environment.getExternalStorageDirectory()+"/test.jpg";
try {
FileOutputStream out = openFileOutput(imagePath, Context.MODE_WORLD_READABLE);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
File file = new File(bitmapPath);
boolean isExists = file.exists();
if (isExists) {
LOGD(TAG, "EXISTS");
} else {
LOGD(TAG, "NOT EXISTS");
}
[UPDATE2]
I found an error below..
java.lang.IllegalArgumentException: File /storage/emulated/0/test.jpg contains a path separator
[UPDATE3]
try {
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(path));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
java.io.FileNotFoundException: /image: open failed: EROFS (Read-only file system)
[UPDATE4]
I put it. But not change.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
[UPDATE5 SOLVED]
I found that the path "imagePath" was correct. (Sorry. I didn't notice it)
String imagePath = Environment.getExternalStorageDirectory() + "/test.jpg";
try {
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(imagePath));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
I believe you are having problems because openFileInput() is for internal storage, not external storage. In fact there is no reason for using Environment.getExternalStorage(). I don't believe the watches have external storage anyway.
Try something like openFileOutput("test.jpg", Context.MODE_WORLD_READABLE); (fyi MODE_WORLD_READABLE is deprecated).
Then use openFileInput("test.jpg") to get it back.
The reason you are getting an error is openFileOutput() cannot have subdirectories.
I'm working with camera in android and I'm new to android camera. I've followed the tutorial from here
Whenever I'll add External storage permission in android manifest, camera saves it in default directory without asking me and I want to save it in my own folder created in sd card. I'd searched a lot but couldn't find any useful link. Please help, any help will be much appreciated. Thank you :)
You can add this code in onActivityResult. This will store your image in a folder named "YourFolderName"
String extr = Environment.getExternalStorageDirectory().toString()
+ File.separator + "YourFolderName";
File myPath = new File(extr, fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
bitMap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(context.getContentResolver(),
bitMap, myPath.getPath(), fileName);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
also need to set permission on Manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
camera saves it in default directory without asking me and I want to save it in my own folder created in sd card
You can tell the camera where you would like the photo to be stored via EXTRA_OUTPUT, as is shown in the documentation's training module on ACTION_IMAGE_CAPTURE.
There is no requirement for all cameras to store images where you ask it to, though. If the file that you ask for does not exist after the image is taken, you will need to fall back to your existing application logic.
You can assign a path to write the image data to specific location like this:
String fileName = "/mnt/sdcard/foldername";
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(data);
I am frustrated searching almost every google page for this problem.
I need to save my bitmap to file.
I have used this method several times with no problem at all. But now I am having a problem.
The Bitmap I am saving is a round image with transparency and having .png format which I have placed in res folder. But I am getting black portion in place of transparent portion.
This is the code I am using for saving the bitmap.
void saveImage(Bitmap bmp) {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, bytes);
finalImg = new File(Environment.getExternalStorageDirectory(),
"emoticon_temp" + ".png");
finalImg.createNewFile();
FileOutputStream fo = new FileOutputStream(finalImg);
fo.write(bytes.toByteArray());
fo.flush();
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
I am saving this bitmap as file because I need to share this image and for sharing the image I need the Stream Uri.
If you got any other Idea for sharing a bitmap, please let me know.
I have 10 images and want to save all this images in android application memory . so whenever any use install this apps , he have already this all images .
i have hard coded many times and not get good response .
is there any way . so i will done it.
please help me
Store all your images in the assets folder of your APK. Once it gets installed, scan the internal memory to see if desired images are there. If not, copy them there. In this way, even when users clear data of your application, you can copy them back. Another good thing is the user will not know anything about it as well, so it a good user-experience. Only thing would be your APK size would increase, so manage accordingly.
Try the below piece of code
InputStream inputStream = getAssets().open("yourfile.jpg");
OutputStream out = new FileOutputStream(new File("/sdcard/yourfile.jpg"));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
inputStream.close();
out.flush();
out.close();
You can keep the files in your asset or res folder , here I have kept the file in res/drawable/ and copy them on sdcard when I require. In below code first we check if file doesn't exist the we create a bitmap from the drawable and write the file out to sdcard.
File file = new File(pathExt+"/Pictures/", "s1.png");
if(isSDCARDMounted()){
if (!file.exists()) {
bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.s1);
try {
FileOutputStream outStream = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
isSDCARDMounted : function for checking if card is mounted or not
pathExt : variable for external storage directory path
Make sure have permission set for writing on external storage
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I have an image that is in the following location (it's in my android workspace resources),
D:\Android\WorkSpace\myprojectname\res\drawable-hdpi
I have used the following line of code to attach this image to an email but it doesn't seem to be working, it sends the email but it won't have the attachment.
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,Uri.parse("android.resource://com.mywebsite.myprojectname/" + R.drawable.image));
this this wrong?
Resources res = this.getResources();
Bitmap bm = BitmapFactory.decodeResource(res, R.drawable.image);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory(), "image.jpg");
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//write the bytes in file
FileOutputStream fo = null;
try {
fo = new FileOutputStream(f);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fo.write(bytes.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Uri uri = Uri.fromFile(f);
After that,
Where u r sending the email, do the following,
intent.putExtra(Intent.EXTRA_STREAM, uri);
It will work for sure. it worked for completely. Try it.
Well, the first problem is that even if that URI format is right (which I doubt), that file would be inside your application's sandbox, which is not accessible to the E-mail activity (or any other activity, for that matter). In any case, you're gonna have to write that file into the SD card and have the email program read it from there. You can output the bitmap using the following code:
Bitmap image = BitmapFactory.decodeResource(getResources(),R.drawable.image);
File file = new File(Environment.getExternalStorageDirectory(), "forEmail.PNG");
OutputStream outStream = new FileOutputStream(file);
image.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
Then use Uri.fromFile() to generate the URI from the file defined above:
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.fromFile(file));
(Code for resource to file adapted from here)
The other answers might work too (they didn't work for me though)! I got it running using only the one line of code bellow, which is very simple and easy. Thanks everyone.
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.image));
I think my issue was that I was putting the package name wrong, but using the getPackageName() method that issue is solved!