Hi
I am writing an Android application in which I want to retrieve the images stored in Gallery folder of Device/emulator. Can any one give me sample code regarding how to achieve this.
Thanks and Regards
If you have path where your images are stored, use this code to get image as bitmap.
FileInputStream in;
BufferedInputStream buf;
try {
in = new FileInputStream("/sdcard/test2.png");
buf = new BufferedInputStream(in);
Bitmap bMap = BitmapFactory.decodeStream(buf);
image.setImageBitmap(bMap);
if (in != null) {
in.close();
}
if (buf != null) {
buf.close();
}
} catch (Exception e) {
Log.e("Error reading file", e.toString());
}
You need to iterate through the path: ex
File f=new File("path of dir");
Now get all files using: f.listFiles();
This is s fair idea..........
Hope it will help........
Just for picking them?. If so, you can use this intent:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
Ger
Related
Ok i'm completely editing this post... I have made it so that I can save the file path to my data base. this works and is saved as /storage/emulated/0/1508blah blah.jpg . Now i cannot get my code to read this item back into a picture.
imagePhoto = (ImageView)findViewById(R.id.detail_recipe_image);
Toast.makeText(this, recipe.image, Toast.LENGTH_SHORT).show();
Bitmap bmp = BitmapFactory.decodeFile(String.valueOf(recipe.image));
imagePhoto.setImageBitmap(bmp);
am I missing something here? cause the Toast Is reading the recipe.image just fine and is displaying the path. why Is the rest not displaying the image?
Storage Code
private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
String picturePath = destination.toString();
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
textImagePath.setText(picturePath.toString());
ImageView img = (ImageView)findViewById(R.id.addphotoview);
img.setImageBitmap(thumbnail);
}
Adding in the files paths seem to be the best solution to the problem i am having so that you #ModularSynth for your help with this. Always making sure all the info is your code to make the file paths work helps.
My application allows users to select an image to upload. When users select an image from a picasa album my data intent comes back with dat=content://com.sec.android.gallery3d.provider/picasa/item/....
Apparently when selecting an image from a picasa folder, I must handle getting the image differently as noted in this answer.
But before I implement a fix, I want to be able to reproduce the crash so I can verify my fix actually works. So how can I get a Picasa folder on my new (marshmallow) Android test device since Picasa has been killed by Google?
The most guaranteed way of getting a file send inside an intent, is to open a stream to it and copy it over to a private folder on your app.
This way works for local file, content uri, picasa, all of it.
Something like that:
private File getSharedFile() {
Uri uri = intent.getExtras().getParcelable(Intent.EXTRA_STREAM);
// or using the new compat lib
Uri uri = ShareCompat.IntentReader(this).getStream();
InputStream is = null;
OutputStream os = null;
try {
File f = ... define here a temp file // maybe getCacheDir();
is = getContentResolver().openInputStream(uri);
os = new BufferedOutputStream(new FileOutputStream(f));
int read;
byte[] bytes = new byte[2048];
while ((read = is.read(bytes)) != -1) {
os.write(bytes, 0, read);
}
return f;
} catch (Exception e) {
... handle exceptions, buffer underflow, NPE, etc
} finally {
try { is.close(); } catch (Exception e) { /* u never know */ }
try {
os.flush();
os.close();
} catch (Exception e) { /* seriously can happen */ }
}
return null;
}
I write a code crop image.The image maybe a little big,so I use intent.putExtra("return-data", false); and intent.putExtra("output", outputUri); So I should decode the uri to get bitmap.Just like this(omActivityResult)
InputStream is = null;
is = getContentResolver().openInputStream(outputUri);
Bitmap avatar = BitmapFactory.decodeStream(is);
I first make External Storage to store the crop image.It work well.
outputUri = Uri.fromFile(new File("/storage/emulated/0/upload.jpg"));
But I need to store it in Internal Storage.I follow the Android developer
to save in Internal Storage.The code follow.
FileOutputStream os = null;
try
{
os = openFileOutput("upload.jpg", 0);
}
catch (FileNotFoundException e)
{
}
finally
{
if (os != null)
{
try
{
os.close();
}
catch (IOException e)
{
}
}
}
outputUri = Uri.fromFile(new File(getFilesDir().getAbsolutePath(), "upload.jpg"));
the avatar will be null.I have read many questions in stackoverflow but it doesn't work. Can you help me?
I am downloading image via a URL using loopj library https://github.com/loopj/android-async-http. In response I get image as File object. I want to store this image as .jpg to my internal storage and get the path of that image. Can anyone please help me regarding this? Or suggest me any other library or the code through which I can achieve this functionality?
Try :
try {
File imgFile = null; //File you received from loopj
FileInputStream fis = new FileInputStream(imgFile);
FileOutputStream fos = new FileOutputStream(
new File("yourPath.jpg"));
byte fileContent[] = new byte[(int) imgFile.length()];
fis.read(fileContent);
fos.write(fileContent);
fis.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
Im trying to save a profile photo, but when I use the save image code I got (i got almost the same example everywhere on the internet), I don't really know where the image is getting stored.
public void saveImage(Bitmap image) {
FileOutputStream out = null;
try {
out = new FileOutputStream("BecityAvatar.png");
image.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
The thing is, I want to store the image so when the profile launches and the layout is loaded, if there is an image stored as the avatar, it loads itself onto the imageview from the layout. If there isn't, nothing will happen. I don't really know where the images will get stored when I save them. Any help or tips will be appreciated.
Your problem is with:
out = new FileOutputStream("BecityAvatar.png");
this way you are creating a FileOutputStream, that points to /, and your application has not right to write there. For instance
File file = new File(getFilesDir(), "BecityAvatar.png")
out = new FileOutputStream(file);
to use getFilesDir(), you need a context