So I am new to Android, but I do not understand why this does not work (and how to get it to work):
ImageView i = (ImageView) findViewById(R.id.image_to_display);
Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
i.setImageURI(uri)
I did a Toast and have made sure that uri.toString() returns a url like content://...
I am also sure that i is a valid reference, because I am successfully able to set it to local images that are part of the .apk.
So why would this not work, and how can I get it to work?
Thanks
If you don't HAVE to have it be an Uri you could do something like this instead.
String fullpath = Environment.getExternalStorageDirectory() + "/pathtoyourfile"
// take the path create a bitmap and populate the ImageView with it.
ImageView iv = (ImageView) v.findViewById(R.id.thumbnail);
Bitmap bm = BitmapFactory.decodeFile(fullpath);
iv.setImageBitmap(bm);
Try loading it yourself and then passing it in:
ContentResolver cr = getContentResolver();
InputStream in = cr.openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(in);
if (bitmap != null) {
i.setImageBitmap(bitmap);
}
Related
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentResolver resolver = getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, "Image" + timestamp + ".jpg");
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/*");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + File.separator + "Compressed_Images");
imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
fos = resolver.openOutputStream(Objects.requireNonNull(imageUri));
bitmap.compress(WEBP_LOSSY, 10, fos);
Objects.requireNonNull(fos);
Got it displayed in imageview------>
InputStream is = getContentResolver().openInputStream(imageUri);
Bitmap bitmap1 = BitmapFactory.decodeStream(is);
compImage.setImageBitmap(bitmap1);
Its Compressing and saving the Image No issue in that
Add an ImageView in the xml layout of your Activity. Like this:
<ImageView
android:id="#+id/my_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Then in your activity's create method you get a reference to that ImageView. Like this:
ImageView myImageView = findViewById(R.id.my_image_view);
And now you call "setImageBitmap" on your imageView with your bitmap as an argument. Like this:
myImageView.setImageBitmap(bitmap); // "bitmap" is your bitmap
If I understood you correctly this should be what you want to do
InputStream is = getContentResolver().openInputStream(imageUri);
Bitmap bitmap1 = BitmapFactory.decodeStream(is);
compImage.setImageBitmap(bitmap1);
Okay....I was managed to set image in Imageview(I hope its Compressed image)
Now how do I get the size of this?
I am new to android studio and stack overflow so I may not make any sense but I want to pass some data from one activity to another. I have managed to do it through the putExtra() on the intent with all the text that I want passed to the activity. However, I do not know how to set a getIntent on an image that has to be produced with setImageResource().
FavActivity
Cursor cursor = (Cursor) mylist.getItemAtPosition(position);
String listName = cursor.getString(1);
String displayName = db.getName(listName);
if (!displayName.isEmpty()) {
String isleName = db.getIsle(listName);
String rowName = db.getRow(listName);
String locationImage = db.getLocationImage(listName);
Intent myIntent = new Intent(view.getContext(),MainActivity.class);
myIntent.putExtra("name", displayName);
myIntent.putExtra("isle", "Isle: " + isleName);
myIntent.putExtra("row", "Row: " +rowName);
myIntent.putExtra("image", locationImage);
startActivity(myIntent);
MainActivity
textView.setText(getIntent().getStringExtra("name"));
textView3.setText(getIntent().getStringExtra("isle"));
textView4.setText(getIntent().getStringExtra("row"));
Image location has to be set like this as its converting from database text
locationView.setImageResource(getResources().getIdentifier(locationImage, "drawable", getPackageName()));
How do I set an intent to do this?
Store the image in a file and pass the file path through the intent to the other activty and you can access the image from that file path in the other activity
You can pass an image but I would suggest storing the image in Internal storage and pass them using Intent.
As of API 24, there is 1 MB restriction else it would throw TransactionTooLarge Exception and may cost you too much memory
Saving image to Internal Storage-
public String createImageFromBitmap(Bitmap bitmap) {
String fileName = "myImage";//no .png or .jpg needed
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
FileOutputStream fo = openFileOutput(fileName, Context.MODE_PRIVATE);
fo.write(bytes.toByteArray());
// remember close file output
fo.close();
} catch (Exception e) {
e.printStackTrace();
fileName = null;
}
return fileName;
}
and then pass that file name in Bundle
Bitmap bitmap = BitmapFactory.decodeStream(context
.openFileInput("myImage"));
An another example to be used
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.my_layout);
Bitmap bitmap = getIntent().getParcelableExtra("image");
ImageView imageView = (ImageView) findViewById(R.id.imageview);
imageView.setImageBitmap(bitmap);
}
Hiii,
You can send an image in Base64 format and convert it to an image after receiving in next Activity.
Though make sure that you can send maximum 50kb data using Intents.
Otherwise, your app might misbehave on sending and receiving data.
I am trying set image in android image view.
I used below code to set image in image view :
Bitmap bmImg = BitmapFactory.decodeFile("http://172.22.30.55:8080/data/product_images/"+data.get("col_1"));
imageView =(ImageView)view.findViewById(R.id.product_image);
imageView.setImageBitmap(bmImg);
But I am getting below exception :
E/BitmapFactory(3663): Unable to decode stream: java.io.FileNotFoundException: /http:/172.22.30.55:8080/data/product_images/056075419be6ed982cb63eba81056396.png: open failed: ENOENT (No such file or directory)
What I am trying to do get the image file from the local server but working my code.
What is my mistake?
Try this:
ImageView imageview= (ImageView)view.findViewById(R.id.product_image);
try{
String url = "http://172.22.30.55:8080/data/product_images/"+data.get("col_1");
URL ulrn = new URL(url);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = con.getInputStream();
Bitmap bmp = BitmapFactory.decodeStream(is);
if (null != bmp)
imageview.setImageBitmap(bmp);
else
System.out.println("Bitmap is NULL");
if (con != null) {
con.disconnect();
}
} catch(Exception e) {
}
Use Picasso to load image from network. you only need to do is write below lines.
String url = "http://172.22.30.55:8080/data/product_images/"+data.get("col_1");
imageView =(ImageView)view.findViewById(R.id.product_image);
Picasso.with (YourActivity.this)
.load(Uri.parse(url))
.into(imageView);
You also need to provide INTERNET permission in your manifest and add dependencies for Picasso in your build.gradle.
In my app I am trying to share a BMP with the ShareActionProvider. It is not working. Am I doing it wrong or do I need to convert it into a PNG (I do not want to deal with files). If so how can I do it? THanks.
Bitmap bmp = qrUtil.create(WIFIQRCODE, pref2);
isCodeGenerated = true;
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_STREAM, bmp);
provider.setShareIntent(intent);
The Intent.EXTRA_STREAM should be a URI pointing to the image file that you want to use; you can't just add a Bitmap there. You should do something like this:
Uri uri = Uri.fromFile(new File(getFilesDir(), "img.jpg"));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri.toString());
This will change depending on the actual location of your image, but that's the general idea.
Trying to set images to image view using the image paths.
My image path
String img_path ="/storage/sdcard0/Fbt/xylo.jpg";
image path code
private String getRealPathFromURI(Uri contentURI) {
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) { // Source is Dropbox or other similar local file path
return contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
return cursor.getString(idx);
}
}
selectedImage = imageReturnedIntent.getData();
File imageFile = new File(getRealPathFromURI(selectedImage));
String path= imageFile.toString();
output =/storage/sdcard0/Fbt/xylo.jpg
I tried all possible code still there is no success
ImageView carpict =(ImageView)findViewById(R.id.img);
Bitmap bitmap = BitmapFactory.decodeFile(img_path);
carpict.setImageBitmap(bitmap);
2.
Uri image22 =(Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+img_path));
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(image22);
Bitmap carpic = BitmapFactory.decodeStream(imageStream);
// carpic = Bitmap.createScaledBitmap(carpic, 72,72, false);
carpict.setImageBitmap(carpic);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
3rd.
carpict.setImageURI
(Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+img_path));
4th.
File imgFile =new File("img_path");
carpict.setImageBitmap(BitmapFactory.decodeFile(imgFile.getAbsolutePath()));
I have also added
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
can anyone tell me where i am going wrong?
try this code
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
System.out.println("sdcard path:"+Environment.getExternalStorageDirectory());
Drawable d = Drawable.createFromPath(Environment.getExternalStorageDirectory()+"/images.png");
imageView.setBackground(d);
imageView.setImageURI(Uri.parse(new File("PathToFile"));
or if you have the file reference:
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
+"/Fleet/xylo.jpg");
imageView.setImageURI(Uri.parse(file));
This should work. You will need to use the file constructor version of Uri.parse to correctly set the image.
Do check if the image is present in this location, or if the image is valid. And please post the logs if this also doesn't work.
The problem was with the image itself when i tried with other images it worked images with formats .jpg .png .jpeg
ImageView carpict =(ImageView)findViewById(R.id.img);
Bitmap bitmap = BitmapFactory.decodeFile(img_path);
carpict.setImageBitmap(bitmap);
Here is the image xylo.jpg which is not getting set, don't know why
http://www.mediafire.com/?lh75aco1en8f3ot
Stirng path="///storage/sdcard0/Android/data/com.package.name/files/Download/Images/one-3.jpg"; //chang with your image file path
File file = new File(path);
imageView.setImageURI(Uri.parse(file.getPath()));