Android get image from internal/external storage - android

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";

Related

Got FileNotFoundException for Environment.getExternalStorageDirectory()

I know there are many post on Environment.getExternalStorageDirectory() for finding out folder for saving files. When I used it, I am getting
/storage/emulated/0/myfolder
as folder path and getting below error when try to use for saving file.
w/System.err: java.io.FileNotFoundException: /storage/emulated/0/myfolder/myfile (No such file or directory)
How to resolve this issue? I would like to use Internal Memory for storing Image & video in the same fashion as other app does.
Thanks in advance for your help.
Update : As requested Code for saving Image that is been created.
public String saveTofolder(Bitmap bitmap, String filename) {
String stored = null;
File sdcard = Environment.getExternalStorageDirectory();
File folder = new File(sdcard.getAbsoluteFile(), "myfolder");
if (!folder.exists()) folder.mkdirs();
File file = new File(folder.getAbsoluteFile(), filename);
if (!file.exists()) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
try {
FileOutputStream fo = new FileOutputStream(file);
fo.write(bytes.toByteArray());
fo.flush();
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
stored = "success";
}
return stored;
}

Get path of a drawable image so that I can post it to the backend

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.

How to create an image file from drawable path

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.

No such file or directory found when trying to load image from database?

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);

Save image in current view to sdcard using Android universal-image-loader

I'm a newbie Android developer. I have loaded an image using universal-image-loader and I would like to save it on my sd card. The file is created in the desired directory with the correct filename, but it always has a size of 0. What am I doing wrong?
A relevant snippet follows:
PS: The image already exists on disk, it's not being downloaded from the Internet.
private void saveImage(String imageUrls2, String de) {
String filepath = Environment.getExternalStorageDirectory()
.getAbsolutePath();
File SDCardRoot = Environment.getExternalStorageDirectory()
.getAbsoluteFile();
String filename = de;
File myDir = new File(SDCardRoot+"/testdir");
Bitmap mSaveBit = imageLoader.getMemoryCache();
File imageFile = null;
try {
//create our directory if it does'nt exist
if (!myDir.exists())
myDir.mkdirs();
File file = new File(myDir, filename);
if (file.exists())
file.delete();
FileOutputStream fileOutputStream = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
bos.flush();
bos.close();
} catch (IOException e) {
filepath = null;
e.printStackTrace();
Toast.makeText(getApplicationContext(),
R.string.diskful_error_message, Toast.LENGTH_LONG)
.show();
}
Log.i("filepath:", " " + filepath);
}
Yes, your code creates an file on sdcard_root/testdir/de only, and didn't write anything to it. Is "imageUrls2" the source image file? If yes, you can open that file with BufferedInputStream, read the data from BufferedInputStream, and copy them to output file with bos.write() before bos.flush() and bos.close().
Hope it helps.

Categories

Resources