set SD card image to ImageView? - android

I have an images in SD card i want to take image from SD card and set in the ImageView.please help me guys i have tried the below code.
My Code:
sendImageFromFolder.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Uri uri=null;
ArrayList<Uri> arrayList=new ArrayList<Uri>();
ImageView imageView= (ImageView) findViewById(R.id.imageView);
File pictues= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String[] listOfPictures=pictues.list();
for(String s:listOfPictures){
uri=Uri.parse("file://"+ pictues.toString() +"/"+s);
arrayList.add(uri);
}
InputStream inputStream = null;
FileOutputStream fileOutputStream=null;
Bitmap bmp;
try {
inputStream = getContentResolver().openInputStream(arrayList.get(0));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
bmp = BitmapFactory.decodeStream(bufferedInputStream);
imageView.setImageBitmap(bmp);
}

File pictureFile = new File("Path to your image");
Bitmap bitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());
mImage.setImage(bitmap);

Related

Issue in downloading image from Firebase Storage and saving it in SD Card

I had uploaded the image on Firebase Storage successfully. I have the URI and using Glide, I'm able to show the image on an ImageView. I want to save this image on my SD card but I'm getting an exception
java.io.FileNotFoundException: No content provider:
https://firebasestorage.googleapis.com/..
In here:
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), myUri);
SaveImage(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
Here is my complete code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_pic);
Intent intent = getIntent();
String str = intent.getStringExtra("pic");
Uri myUri = Uri.parse(str);
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), myUri);
SaveImage(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
ImageView imageView = (ImageView)findViewById(R.id.displayPic);
Glide.with(getApplicationContext()).load(myUri)
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView);
}
private void SaveImage(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
URI looks like this:
https://firebasestorage.googleapis.com/example.appspot.com/o/pics%2Fc8742c7e-8f59-4ba3-bf6f-12aadfdf4a.jpg?alt=media&token=9bsdf67d-f623-4bcf-95d7-5ed97ecf1a21
Using Glide Try this.
Bitmap bitmap= Glide.
with(this).
load(mDownloadUrl).
asBitmap().
into(100, 100). // Width and height
get();
SaveImage(bitmap);
where mDownloadUrl is your image URL.
Firebase Storage does not have a registered content resolver. The download Url you get is actually a plain vanilla https:// Url that you can feed into Glide.
You can also download this Url directly. Check out this question.
Just call downloadUri.toString() to get the download Url in string form.

How to save an image attached to the image view?

I am using a Glide library for loading remote URLs into ImageView's.
I want to save the image from this ImageView to gallery. (I don't want to make another network call again to download the same image).
How we can achieve this?
Glide.with(yourApplicationContext))
.load(youUrl)
.asBitmap()
.into(new SimpleTarget<Bitmap>(myWidth, myHeight) {
#Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
//set bitmap to imageview and save
}
};
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, false);
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 60, byteArrayOutputStream);
String fileName = "image.jpeg";
File file = new File("your_directory_path/"
+ fileName);
try {
file.createNewFile();
// write the bytes in file
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(byteArrayOutputStream.toByteArray());
// remember close the FileOutput stream
fileOutputStream.close();
ToastHelper.show(getString(R.string.qr_code_save));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ToastHelper.show("Error");
}
Note : If your drawble is not always an instanceof BitmapDrawable
Bitmap bitmap;
if (mImageView.getDrawable() instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
} else {
Drawable d = mImageView.getDrawable();
bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Try this
I haven't try this way. But i think this match your problem. Put this code on onBindViewHolder of your RecyclerView adapter.
Glide.with(yourApplicationContext))
.load(youUrl)
.asBitmap()
.into(new SimpleTarget<Bitmap>(myWidth, myHeight) {
#Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
//Set bitmap to your ImageView
imageView.setImageBitmap(bitmap);
viewHolder.saveButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//Save bitmap to gallery
saveToGallery(bitmap);
}
});
}
};
This might help you
public void saveBitmap(ImageView imageView) {
Bitmap bitmap = ((GlideBitmapDrawable) imageView.getDrawable()).getBitmap();
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/My Images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
File file = new File(myDir, fname);
if (file.exists()) file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception ex) {
//ignore
}
}

How to save the image?

I am using Picasso to fetch image and I just want to save the image.Below code is not working for me to save the image.
public class DownloadImage extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mindmaps);
final ImageView imageView = (ImageView) findViewById(R.id.imageView);
Picasso.with(this)
.load("http://i.imgur.com/DvpvklR.png")
.into(imageView);
final Button btntakephoto = (Button) findViewById(R.id.save);
btntakephoto.setOnClickListener((View.OnClickListener) this);
}
public void onClick(View v){
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Drawable image = imageView.getDrawable();
if (image != null && image instanceof BitmapDrawable) {
BitmapDrawable drawable = (BitmapDrawable) image;
Bitmap bitmap = drawable.getBitmap();
try {
File file = new File("path where you want to save");
FileOutputStream stream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, stream);
stream.flush();
stream.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
On pressing the save button image should be saved in the gallery.
MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);
try this,
call this method from button click:
private void saveImage() {
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Drawable image = imageView.getDrawable();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), image);
OutputStream out = null;
String fileName = "MyImage.jpg";
String mPath = "";
try {
out = mActivity.openFileOutput(fileName, Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
File f = mActivity.getFileStreamPath(fileName);
mPath = f.getAbsolutePath();
} catch (Exception e) {
e.printStackTrace();
}
Logger.logger("URI :" + mPath);
}
Make your DownloadImage class implement View.OnClickListener interface.

Get last captured image?

I have an application, where you can take a picture about yourself (the app saves the image in a specified folder called "MyAppImage"), and I want to display the taken image in a second activity with a code, how to do this? I want to display it in a imageView in my SecondActivity, but I need a code that can get the last captured camera image from this folder, is there any way to do this?
Hope someone can guide me, how to do this, thanks!
After take photo:
MainActivity
String filePath;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
}
private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
String fileName = "yourPhotoName" + ".jpg"
filePath = "pathOfMyAppImageFolder" + fileName;
File destination = new File(filePath);
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();
}
}
yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), AnotherActivity.class);
intent.putExtra("filePath", filePath)
startActivity(intent);
}
});
AnotherActivity
String filePath;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = this.getIntent();
filePath = intent.getStringExtra("filePath");
File imgFile = new File(filePath);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
}
}
Take the last photo of the folder:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<File> files = getListFiles(new File("MyAppImageFolderPath"));
File imgFile = files.get(files.size());
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
}
}
private List<File> getListFiles(File parentDir) {
ArrayList<File> inFiles = new ArrayList<File>();
File[] files = parentDir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
inFiles.addAll(getListFiles(file));
} else {
if(file.getName().endsWith(".jpg")){ //change to your image extension
inFiles.add(file);
}
}
}
return inFiles;
}
Once you takePhoto, save it into file.
Then call intent to start second activity and putExtraString with your image file path.
That is all.

Webservice url images store in to android Gallery

I am using ImageView in my android application here i show the images from webservice so i am using UrlImageViewHelper. i want to store this image into android Gallery files.
my images like:
String Images = dataExtra.get("images").toString();
System.out.println("image URL"+Images);
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(Images);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
i tried like this.. but its not working. Any one can help me how to store these Images into Android Gallery?
i got solution for this problem, Here my answer
private void saveImagesIntoGallery(){
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
// File sdCardDirectory = Environment.getExternalStorageDirectory();// its stores under sdcard not in a specific path
String sdCardDirectory = Environment.getExternalStorageDirectory().toString()+"/Pictures/";
String url = arrayForImages[i].toString();
String file = url.substring(url.lastIndexOf('/')+1);
System.out.println("PATH NAME"+sdCardDirectory);
File image = new File(sdCardDirectory, file);
boolean success = false;
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
Toast.makeText(getApplicationContext(), "Image saved with success",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error during image saving", Toast.LENGTH_LONG).show();
}
}
Are you sure that the gallery is the best place to store images of webservice?
If you wanted to save to internal storage:
public void saveBitmap(String name, Bitmap bitmap){
if(bitmap!=null && name!=null){
FileOutputStream fos;
if(bitmap!=null){
try {
fos = openFileOutput(name, Context.MODE_PRIVATE);
bitmap.compress(CompressFormat.JPEG, 90, fos);
} catch (FileNotFoundException e) {}
}
}
}
To gallery, i have not examples here. But search a little;)

Categories

Resources