Set Image using getAbsolutePath() - android

I'm trying to use custom gallery and trying to get the selected images and would like to display it in listview
This is what I'm trying to do:
if (extras != null) {
for (int i = 0; i < fetchList.size(); i++) {
Bitmap originBitmap = null;
filepath = fetchList.get(i);
newStringList.add(filepath);
imgfilename = filepath.substring(filepath.lastIndexOf("/") + 1);
Uri selectedImage = Uri.fromFile(new File(filepath));
File myFile = new File(selectedImage.getPath());
myFile.getAbsolutePath();
Toast.makeText(MainActivity.this,myFile.getAbsolutePath(),Toast.LENGTH_LONG).show();
listimage.setImageBitmap(myFile.getAbsolutePath());
myStringList.add(imgfilename);
arrayAdapter = new ArrayAdapter<String>(
this,
R.layout.custom_textview, R.id.listtext,
myStringList);
lv.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
}
}
Now my question is how do I set the image to the image view using this path ?
If I use this listimage.setImageBitmap(myFile.getAbsolutePath()); I couldnt see the image.
Path I'm getting is : /storage/emulated/0/

Pls try this way,
File imgFile = new File("/sdcard/Images/test_image.jpg");
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
}
Hope this will help you.

Try this
private AQuery aq = new AQuery(activity);
File imgFile = new File(filepath);
aq.id(imageView).image(imgFile, false, 300, new BitmapAjaxCallback() {
#Override
public void callback(String url, ImageView iv, Bitmap bm, AjaxStatus status) {
iv.setImageBitmap(bm);
}
});

Related

How do I save the imageview created to camera roll?

In the code below, you can see that I have created an imageview using a bitmap. What I want to know is how I can save an image of that imageview to my camera roll. Thanks!
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.key_code_zoom);
title = (TextView) findViewById(R.id.accountTitleLarge);
imageView = (ImageView) findViewById(R.id.keyCodeLarge);
Intent callingActivity = getIntent();
Bundle callingBundle = callingActivity.getExtras();
if (callingBundle != null) {
String titleText = callingBundle.getString("title");
byte[] bytes = getIntent().getByteArrayExtra("bitmapbytes");
bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
title.setText(titleText);
imageView.setImageBitmap(bmp);
}
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
supportFinishAfterTransition();
}
});
}
To save an image in gallery, you must first get the bitmap and then save it.
private void imageToRoll(){
imageView.buildDrawingCache();
Bitmap image = imageView.getDrawingCache(); // Gets the Bitmap
MediaStore.Images.Media.insertImage(getContentResolver(), imageBitmap, imagTitle , imageDescription); // Saves the image.
}
Also, set the permission in your manifest.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
If you are looking for some code that will help you save the Image from the ImageView to your phone storage then the following code will help you
public String getImageFromView() {
imageview.setDrawingCacheEnabled(true);
// this is the important code :)
// Without it the view will have a dimension of 0,0 and the bitmap will be null
imageview.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
imageview.layout(0, 0, imageview.getMeasuredWidth(), imageview.getMeasuredHeight());
imageview.buildDrawingCache(true);
//Define a bitmap with the same size as the view
Bitmap b = imageview.getDrawingCache();
String imgPath = getImageFilename();
File file = new File(imgPath);
try {
OutputStream os = new FileOutputStream(file);
b.compress(Bitmap.CompressFormat.JPEG, 90, os);
os.flush();
os.close();
ContentValues image = new ContentValues();
image.put(Images.Media.TITLE, "NST");
image.put(Images.Media.DISPLAY_NAME, imgPath.substring(imgPath.lastIndexOf('/')+1));
image.put(Images.Media.DESCRIPTION, "App Image");
image.put(Images.Media.DATE_ADDED, System.currentTimeMillis());
image.put(Images.Media.MIME_TYPE, "image/jpg");
image.put(Images.Media.ORIENTATION, 0);
File parent = file.getParentFile();
image.put(Images.ImageColumns.BUCKET_ID, parent.toString()
.toLowerCase().hashCode());
image.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, parent.getName()
.toLowerCase());
image.put(Images.Media.SIZE, file.length());
image.put(Images.Media.DATA, file.getAbsolutePath());
Uri result = mContext.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image);
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return imgPath;
}
you will have to provide a file path getImageFilename();this function provides the path where the file is suppose to be stored. The ContentValues will be responsible to let the images be scanned in gallery.
Hope this helps.

set SD card image to ImageView?

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

setImageViewUri doesn't show picture but show background of imageview

File file = new File("/storage/emulated/0/BabyCareData/photo/20160229_161413.jpg");
if (file.exists()) {
views.setImageViewUri(R.id.imageAvatar, Uri.parse(file.getPath()));
}
I have check the path and the uri,it is right.setImageViewUridoesn't show picture but show a white screen(background is white).
Try this:
File file = new File("/storage/emulated/0/BabyCareData/photo/20160229_161413.jpg");
if (file.exists()) {
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
views.setImageViewBitmap(R.id.imageAvatar, bitmap);
}
Try this:
File file = new File(/storage/emulated/0/BabyCareData/photo/20160229_161413.jpg);
if(file.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
setBitmap(views,R.id.imageAvatar,myBitmap);
}
private void setBitmap(RemoteViews views, int resId, Bitmap bitmap){
Bitmap proxy = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(proxy);
c.drawBitmap(bitmap, new Matrix(), null);
views.setImageViewBitmap(resId, proxy);
}

How to set Image on ImageButton dynamically?

I want to set an image on a button in my app, dynamically from a file on the sdcard. I have tried this code but it is not working. I have tried to convert the image to a bitmap object and I set that object to ImageButton, but it isn't showing anything. How can I solve this issue?
My code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File imageFile = new File("/sdcard0/DCIM/camera/jbn.jpg");
Bitmap bmp = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
ImageButton button1 = (ImageButton)findViewById(R.id.imgBtn);
button1.setImageBitmap(bmp);
}
XML
<ImageButton
android:layout_width="200dip"
android:layout_height="200dip"
android:id="#+id/imgBtn"
/>
Algorithm
void loadPic()
{
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String pathName = baseDir + "/DCIM/camera/";
File parentDir=new File(pathName);
File[] files = parentDir.listFiles();
Date lastDate;
String lastFileName;
boolean isFirstFile = true; //just temp variable for being sure that we are on the first file
for (File file : files) {
if(isFirstFile){
lastDate = new Date(file.lastModified());
isFirstFile = false;
}
if(file.getName().endsWith(".jpg") || file.getName().endsWith(".jpeg")){
Date lastModDate = new Date(file.lastModified());
if (lastModDate.after(lastDate)) {
lastDate = lastModDate;
lastFileName = file.getName();
}
}
}
Try with something simple like this for example:
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "jbn.jpg";
String pathName = baseDir + "/your/folder(s)/" +_ fileName; //maybe your folders are /DCIM/camera/
Bitmap bmp = BitmapFactory.decodeFile(pathName);
ImageButton button1 = (ImageButton)findViewById(R.id.imgBtn);
button1.setImageBitmap(bmp);
Try to get get AbsolutePath of image file :
File imageFile = new File("/sdcard0/DCIM/camera/jibin.jpg");
Bitmap bmp = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
You can try something like this -
String path = Environment.getExternalStorageDirectory()
+ "/Images/test.jpg";
File imgFile = new File(path);
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile
.getAbsolutePath());
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(myBitmap);
}
If you want to set the image dynamically from any URL that you have, set the image in this way. You can also set the bitmap width and height.
private class LoadImage extends AsyncTask<Bundle, String, Bitmap> {
Bitmap bitmap;
#Override
protected Bitmap doInBackground(Bundle... args) {
extras=args[0];
try {
InputStream in = new java.net.URL("Enter your URL").openStream();
bitmap = BitmapFactory.decodeStream(in);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
protected void onPostExecute(Bitmap image) {
imageButton.setImageBitmap(image);
}
}
And if you want to set the image from the local directory or from the resources folder then just fetch the Image from the folder and set that in image and you don't need to convert it into a Bitmap.
Thanks

Image is does not show in ImageView dynamically from sdcard

In My Code The image is not open in ImageView from sdcard i already checked the permissions in 'manifest.xml'.
Same if i try to open it using static name then it will showed by ImageView but not dynamically.
Main Activity
private OnClickListener OnCapture = new OnClickListener() {
#Override
public void onClick(View v) {
String time = mCamUtils.clickPicture();
nextActivity = new Intent(MainActivity.this,EffectActivity.class);
nextActivity.putExtra("ImageName", time);
startActivity(nextActivity);
finish();
}
};
EffectActivity
public class EffectActivity extends Activity {
Intent getActivity = null;
ImageView image = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.effectactivity);
getActivity = getIntent();
String name = getActivity.getStringExtra("ImageName");
String path = Environment.getExternalStorageDirectory()+"/GalaxyPhoto/GX_" +name+ ".JPG";
Toast.makeText(getApplicationContext(), path, Toast.LENGTH_LONG).show();
image = (ImageView) findViewById(R.id.imageView1);
File f = new File(path);
byte[] b = new byte[(int)f.length()];
if(f.exists())
{
try
{
if(f.exists())
{
FileInputStream ff = new FileInputStream(f);
ff.read(b);
Bitmap g = BitmapFactory.decodeFile(path);
image.setImageBitmap(g);
ff.close();
}
else
{
Toast.makeText(getApplicationContext(), "ELSE", Toast.LENGTH_LONG).show();
}
}
catch(Exception e)
{
}
}
}
}
I am trying to open image which i saved in before this activity and i catch the name of image here but that does not show the image.
String path = Environment.getExternalStorageDirectory()+"/GalaxyPhoto/GX_" +name+ ".JPG";
In this line try changing "JPG" into "jpg".i think your file extension might be in lowercase letter and it might say f.exists false.
Hope it will help.
String fname = "Pic-" + System.currentTimeMillis() + ".png";
File image= new File(imagesFolder, fname);
Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
picturePath=image.getAbsolutePath();
bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(), bitmapOptions);
imageView.setImageBitmap(bitmap);
try this code

Categories

Resources