I'm trying to create a Bitmap or Drawable from existing file path.
String path = intent.getStringExtra("FilePath");
BitmapFactory.Options option = new BitmapFactory.Options();
option.inPreferredConfig = Bitmap.Config.ARGB_8888;
mImg.setImageBitmap(BitmapFactory.decodeFile(path));
// mImg.setImageBitmap(BitmapFactory.decodeFile(path, option));
// mImg.setImageDrawable(Drawable.createFromPath(path));
mImg.setVisibility(View.VISIBLE);
mText.setText(path);
But setImageBitmap(), setImageDrawable() doesn't show an image from the path. I've printed path with mText and it looks like : /storage/sdcard0/DCIM/100LGDSC/CAM00001.jpg
What am i doing wrong? Anyone can help me?
Create bitmap from file path:
File sd = Environment.getExternalStorageDirectory();
File image = new File(sd+filePath, imageName);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
imageView.setImageBitmap(bitmap);
If you want to scale the bitmap to the parent's height and width then use Bitmap.createScaledBitmap function.
I think you are giving the wrong file path.
It works for me:
File imgFile = new File("/sdcard/Images/test_image.jpg");
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
//Drawable d = new BitmapDrawable(getResources(), myBitmap);
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
}
Edit:
If above hard-coded sdcard directory is not working in your case, you can fetch the sdcard path:
String sdcardPath = Environment.getExternalStorageDirectory().toString();
File imgFile = new File(sdcardPath);
here is a solution:
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
Well, using the static Drawable.createFromPath(String pathName) seems a bit more straightforward to me than decoding it yourself... :-)
If your mImg is a simple ImageView, you don't even need it, use mImg.setImageUri(Uri uri) directly.
For Drawable -
Drawable drawable = Drawable.createFromPath(your path in string);
For Bitmap -
Bitmap bitmap = BitmapFactory.decodeFile(your path in string);
How simple it was hope you like
static ArrayList< Drawable> d;
d = new ArrayList<Drawable>();
for(int i=0;i<MainActivity.FilePathStrings1.size();i++) {
myDrawable = Drawable.createFromPath(MainActivity.FilePathStrings1.get(i));
d.add(myDrawable);
}
you can't access your drawables via a path, so if you want a human readable interface with your drawables that you can build programatically.
declare a HashMap somewhere in your class:
private static HashMap<String, Integer> images = null;
//Then initialize it in your constructor:
public myClass() {
if (images == null) {
images = new HashMap<String, Integer>();
images.put("Human1Arm", R.drawable.human_one_arm);
// for all your images - don't worry, this is really fast and will only happen once
}
}
Now for access -
String drawable = "wrench";
// fill in this value however you want, but in the end you want Human1Arm etc
// access is fast and easy:
Bitmap wrench = BitmapFactory.decodeResource(getResources(), images.get(drawable));
canvas.drawColor(Color .BLACK);
Log.d("OLOLOLO",Integer.toString(wrench.getHeight()));
canvas.drawBitmap(wrench, left, top, null);
Related
I am trying to convert Java code into c#
So here is Java code
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap decodeStream = BitmapFactory.decodeStream(openInputStream, null, options);
Then I am saving this bitmap by
File appDirectory= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File dest = new File(appDirectory, "yourImage.jpg");
try (FileOutputStream out = new FileOutputStream(dest)) {
decodeStream.compress(Bitmap.CompressFormat.JPEG, 100, out); // bmp is your Bitmap instance
} catch (IOException e) {
e.printStackTrace();
}
Now can anyone help me to convert this code into c#. I Tried to import Xamrine Android DLL but got success no far.
I am
As far as I understand you only need to load simple jpg image. That is all your java code do.
If you want to load jpg image from stream you can use
Bitmap.FromStream()
e.g.
using (FileStream fs = new FileStream(#"Image Address.jpg", FileMode.Open, FileAccess.Read))
{
var decodeStream = Bitmap.FromStream(fs);
}
of course you can open your image without stream too.
var image = Bitmap.FromFile(#"Image Address.jpg");
So your code will be something like this
File appDirectory= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File dest = new File(appDirectory, "yourImage.jpg");
var image = Bitmap.FromFile(dest.FullName);
You can not access Bitmap class out of the box.
For dot net core install package System.Drawing.Common.
For dot net framework add reference to System.Drawing.
For Xamarin see this answer https://stackoverflow.com/a/34869330/5964792
I need to display an image which I know the file name but I don't know the folder, which must be specified by the user.
I use Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); to get the folder from the user. It returns an Uri with a path along these lines:
content://com.android.externalstorage.documents/tree/primary%3ADownload
Now I need to display an image from this folder in an ImageView. I tried the following:
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),
Uri.parse(chosenFolder + "/image.png"));
ImageView imageView = findViewById(R.id.imageview);
imageView.setImageBitmap(bitmap);
The first line throws the following exception:
java.lang.IllegalArgumentException: Invalid URI: content://com.android.externalstorage.documents/tree/primary%3ADownload/image.png
Replacing %3A by : doesn't work.
How to correctly set the Uri to display the image?
i think you should use file path and not URI . A content:// Uri does not have to represent a file on the filesystem .
try this :
String filePath = null;
if (chosenFolder != null && "content".equals(chosenFolder.getScheme())) {
Cursor cursor = this.getContentResolver().query(chosenFolder, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
filePath = cursor.getString(0);
cursor.close();
} else {
filePath = chosenFolder.getPath();
}
and then create bitmap with file path :
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),
Uri.parse(filepath+ "/image.png"));
I managed to achieve what I wanted with an InputStream.
InputStream is = getContentResolver().openInputStream(imageUri);
Bitmap bitmap = BitmapFactory.decodeStream(is);
To work, the imageUri must be an Uri along the lines of:
content://com.android.externalstorage.documents/document/primary:Download/image.png
However, this means you will be accessing files you do not have permission by default. This means you will have to deal with getting permission.
This is not what I wanted so I scrapped this approach.
I have image in my ListView. They are loaded as follows:
String iMages[] = {
"http://www.thebiblescholar.com/android_awesome.jpg",
"http://blogs-images.forbes.com/rogerkay/files/2011/07/Android1.jpg",
"http://cdn.slashgear.com/wp-content/uploads/2012/10/android-market-leader-smartphone.jpg",
"http://www.planmyworkshop.com/images/android.jpeg",
"http://www.androidguys.com/wp-content/uploads/2012/07/01-android2.jpg"
};
ArrayList<Bitmap> bitmap_array = new ArrayList<Bitmap>();
for (int i = 0; i < iMages.length; i++) {
Log.d("i-->" + i, "Url-->" + iMages[i]);
Bitmap bit = getBitmapFromURL(iMages[i]);
bitmap_array.add(bit);
}
How load them from res/drawable ? I tried different ways, but all the way past ...
Try something like this to decode a bitmap from your drawable folder:
Bitmap bitmap= BitmapFactory.decodeResource(context.getResources(),
R.drawable.ic_launcher);
I assume the images in your ListView are of type ImageView or subclasses (ImageButton, ZoomButton etc.).
If that is the case, just set the res image as background:
myImageView.setBackgroundResource(R.drawable.my_image);
Remember to do it only from UI thread.
You can also do like this
String imageFileName = "launcher"; // this is image file name
String PACKAGE_NAME = getApplicationContext().getPackageName();
int imgId = getResources().getIdentifier(PACKAGE_NAME+":drawable/"+imageFileName , null, null);
image_view.setImageBitmap(BitmapFactory.decodeResource(getResources(),imgId));
I'm trying to load an image from external storage. I set the permissions, I tried different ways, but none of them works.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(file.toString());
tv.setImageBitmap(bitmap);
and this one,
FileInputStream streamIn = new FileInputStream(file);
Bitmap bitmap = BitmapFactory.decodeStream(streamIn);
tv.setImageBitmap(bitmap);
streamIn.close();
If i have file abc.jpg on the sdcard then:
String photoPath = Environment.getExternalStorageDirectory() + "/abc.jpg";
and to get bitmap.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
or
Bitmap bitmap1 = BitmapFactory.decodeFile(photoPath);
to avoide out of memory error I suggest you use the below code...
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
final Bitmap b = BitmapFactory.decodeFile(photoPath, options);
To avoid above issue you can use Picasso (A powerful image downloading and caching library for Android)
Documentation
How To?
Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);
Picasso.with(context).load(new File(...)).into(imageView3);
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard.getAbsolutePath() + "/Pictures");
File file = new File(directory, "image_name.jpg"); //or any other format supported
FileInputStream streamIn = new FileInputStream(file);
Bitmap bitmap = BitmapFactory.decodeStream(streamIn); //This gets the image
streamIn.close();
Get the path of the image from your folder as below. And then decode the file as a bitmap.
File file= new File(android.os.Environment.getExternalStorageDirectory(),"Your folder");
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath())
If you have a file path, just use BitmapFactory directly, but tell it to use a format that preserves alpha:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
selected_photo.setImageBitmap(bitmap);
There is a function called
createFromPath(String)
in the Drawable class.
So the statement
String path="/storage/..<just type in the path>";
Drawable.createFromPath(path);
will return a drawable object
I cant get this to work anymore
Here is my code:
ImageView jpgView = (ImageView)findViewById(R.id.ImageView01);
String myJpgPath = "/sdcard/pic.jpg";
jpgView.setVisibility(View.VISIBLE);
//jpgView.setScaleType(ImageView.ScaleType.FIT_CENTER);
jpgView.setAdjustViewBounds(true);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bm = BitmapFactory.decodeFile(myJpgPath, options);
jpgView.setImageBitmap(bm);
Can anyone help?
You can just use bitmapdrawable, e.g.
ImageView jpgView = (ImageView)findViewById(R.id.ImageView01);
String myJpgPath = "/sdcard/pic.jpg";
BitmapDrawable d = new BitmapDrawable(getResources(), myJpgPath);
jpgView.setImageDrawable(d);
The image won't be shown if you open the emulator directly from the eclipse, by clicking run. You'll be able to access the SD card if you start the emulator using the command line, then click your app on the emulator.