hi everyone i'm an android developer and i'm trying to create an image file from a drawable resource, like this :
Bitmap btm = BitmapFactory.decodeResource(getResources(),R.drawable.image1);
Bitmap btm = BitmapFactory.decodeFile(pathname);
btm.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory()+ File.separator + "temporary_file.jpg");
try {
f.createNewFile();
new FileOutputStream(f).write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
But what i need is create the file starting from the path of image1,
I tried to use :
String pathname = currentLevel.getImagePath();
Bitmap btm = BitmapFactory.decodeFile(pathname);
btm.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
But it does not work:
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: android.resource:/com.example.utente.guessimageferes/drawable/immagine1 (No such file or directory)
also with
String pathname ="android.resource://com.example.utente.guessimageferes/drawable/immagine1"
i have the same problem it's possible make that i want?
what i need is starting from the path of image1
There is no path to image1. image1 is a resource. It is a file on your development machine. It is not a file on the device. Hence, it does not have a path.
Related
I want to save an image to SECONDARY_STORAGE (Not saved to phone memory, which android understands is SdCard ), this is code:
In the manifests file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
Java code:
public void Clickdownload(View v) {
Bitmap myImage = GetImageBitmapFromUrl();
String path = System.getenv("SECONDARY_STORAGE") + "/";
OutputStream out = null;
try {
File file = new File(path, "nameImage.jpg");
out = new FileOutputStream(file);
myImage.compress(Bitmap.CompressFormat.JPEG, 85, out);
out.flush();
out.close();
} catch (Exception e) {
}
}
When I run with Debug I get this error
Error at:
myImage.compress(Bitmap.CompressFormat.JPEG, 85, out);
and it didn't save.
This is the code for reading image from sd card :
String path =System.getenv("SECONDARY_STORAGE")+"/myImage/";
File imgFile = new File(path);
if(imgFile.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView imageView=(ImageView)findViewById(R.id.imageView);
imageView.setImageBitmap(myBitmap);
}
When I use:
String path = Environment.getExternalStorageDirectory().toString();
I can save an image to the phone memory.
I can save images from my camera and copy images from my computer on my SdCard, so I do not think this is a Read only SdCard. I tried with many types SdCards but it does not work.
I am using android 5.1.1.
I have a set of icons stored in the drawable folder.
Based on users selection I'll be posting these icons to the backend.
When I try to retrieve the path for these drawables, I get the following error
HTTP FAILED: java.io.FileNotFoundException: (No such file or directory)
This is how I am getting the path
public String getURLForResource (int resourceId) {
return Uri.parse("android.resource://"+ BuildConfig.APPLICATION_ID+"/" +resourceId).toString();
}
File fileToUpload = new File(getURLForResource(R.drawable.icon));
I have referred the following link says that it's not possible to retrieve the path for a drawable.
Any idea on how I can solve this problem?
As solution you can write your drawable to file.
Convert drawable to bitmap
Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.your_drawable);
Save bitmap to file
String path = Environment.getExternalStorageDirectory().toString();
File file = new File(path, "image" + System.currentTimeMillis() + ".jpg");
try (FileOutputStream out = new FileOutputStream(file)) {
bm.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush(); // Not really required
out.close(); // do not forget to close the stream
} catch (IOException e) {
e.printStackTrace();
}
You can use file now.
I all, I have saved my image to the external directory like this:
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Images");
imagesFolder.mkdirs();
image = new File(imagesFolder.getPath(), "MyImage_" + timeStamp + ".jpg");
fileUri = Uri.fromFile(image);
Then save the image in the database (SQLite) like this:
contentValues.put(LocationsDB.FIELD_FILEPATH, fileUri.toString());
The when loading the image to imageView:
String theimage = null;
.....
theimage = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_FILEPATH));
.....
final Bitmap myImage = BitmapFactory.decodeFile(theimage);
ImageView image = (ImageView) findViewById(R.id.PicView);
image.setImageBitmap(myImage);
But when running the app i get this error:
01-10 12:12:26.943: E/BitmapFactory(30607): Unable to decode stream: java.io.FileNotFoundException: /file:/storage/emulated/0/Images/MyImage_20150110_121217.jpg: open failed: ENOENT (No such file or directory)
What am i doing wrong here? I thought that this would get the image and display the image?
First of all make sure that your images store in Image folder
But your problem is fromFile() method ,it adds file:// to the absolute path of your images ,so BitmapFactory can't find your images and throw java.io.FileNotFoundException.
Your image address is
file:///storage/emulated/0/Images/MyImage_1234.jpg
instead of
/storage/emulated/0/Images/MyImage_1234.jpg
Try to change
contentValues.put(LocationsDB.FIELD_FILEPATH, fileUri.toString());
to
contentValues.put(LocationsDB.FIELD_FILEPATH, image.getAbsolutePath());
The problem is that new File(), doesn't create a file. It creates a pointer to that location.
You need to use an OutputStream to save that file:
// Open file
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Images");
imagesFolder.mkdirs();
File image = new File(imagesFolder.getPath(), "MyImage_.jpg");
// Create it if it doesn't exist
if (!image.exists()) {
try {
// Create file
FileOutputStream outputStream = new FileOutputStream(image);
outputStream.write("Hello".getBytes());
outputStream.close();
// Save ABSOLUTE_PATH to DB
contentValues.put(LocationsDB.FIELD_FILEPATH, image.getAbsolutePath());
...
} catch (IOException e) {
e.printStackTrace();
}
}
Then fetch the path from DB and use
String absPath = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_FILEPATH));
final Bitmap myImage = BitmapFactory.decodeFile(absPath);
I try to show my created png file in an imageView. When I start my program and click the showCreatedPng button, there is just a white blank page on emulator's screen.
And logCat says : (Maybe Logcat shows errors because of Emulator. My emulator has 200 mb sd-card but, I can not find where is the created PNG in my computer. When I start the program my phone, PNG saved in GT-I9100\Phone folder. I guess the folder is an internal folder. Anyway, I can not see my created png file. Please help.)
05-21 21:53:39.764: E/BitmapFactory(1335): Unable to decode stream: java.io.FileNotFoundException: /mnt/sdcard/*.png: open failed: ENOENT (No such file or directory)
These are codes that I used.
For saving :
private void saveImageToExternalStorage(Bitmap bitmap) {
String qrPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
try
{
File dir = new File(qrPath);
if (!dir.exists()) {
dir.mkdirs();
}
OutputStream fOut = null;
File file = new File(qrPath, "QRCode.png");
if(file.exists()){
file.delete();
file.createNewFile();
fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
}
}
catch (Exception e) {
Log.e("saveToExternalStorage()", e.getMessage());
}
}
And codes for get png file :
String qrPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/*.png";
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap mustOpen = BitmapFactory.decodeFile(qrPath, options);
setContentView(R.layout.qr_image);
ImageView imageView = (ImageView) findViewById(R.id.qr_image);
imageView.setImageBitmap(mustOpen);
Thanks for your help.
You are trying to open a file called *.png. This should probably be QRCode.png, thus the code
Environment.getExternalStorageDirectory().getAbsolutePath() + "/QRCode.png";
I created canvas that can be used to draw some shapes on it.
How can I save its content to PNG file on user's SD card?
check out this link this link
In this link you can find the method
void saveImage() {
try {
String filename = Environment.getExternalStorageDirectory().toString();
File f = new File(filename ,"myImage.png");
f.createNewFile();
System.out.println("file created " + f.toString());
FileOutputStream out = new FileOutputStream(f);
Bitmap bitmap = showImage(urlStr);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
}
which is used to save the image that you got into a bitmap. and check this link for getting bitmap from canvas
hope this helps you.
Happy coding
Canvas is just a means to draw to the Bitmap.
You should have created Canvas with new Canvas(myBitmap);. So when you draw on the Canvas, it draws to your bitmap.
so using myBitmap Do the following (code here:
String fileName = Environment.getExternalStorageDirectory() + "/test.png";
OutputStream stream = new FileOutputStream(fileName);
/* Write bitmap to file using JPEG or PNG and 80% quality hint for JPEG. */
myBitmap.compress(CompressFormat.PNG, 80, stream);
stream.close();