In my program, I have to change image path to bitmap. The image path is already exit but when I change to bitmap, bitmap always show null. I don't know what happen.Here is my code.
String dirName = Environment.getExternalStorageDirectory().toString()+picturePath;
File sddir = new File(dirName);
Bitmap myBitmap = BitmapFactory.decodeFile(sddir.getAbsolutePath());
//Drawable d = new BitmapDrawable(getResources(), myBitmap);
Log.i("mybitmap",myBitmap+"");
Log.i("dirName",dirName+"");
Log.i("FileName",sddir+"");
Please, give me some advice...
Edit: Logcat output:
01-19 11:56:18.085: I/mybitmap(1469): null
01-19 11:56:18.085: I/dirName(1469): /mnt/sdcard/mnt/sdcard/447650.jpg
01-19 11:56:18.085: I/FileName(1469): /mnt/sdcard/mnt/sdcard/447650.jpg
01-19 12:19:59.754: I/PicturePath(1671): /mnt/sdcard/62afbdb0c0d287195c0eb7793427b8b8.jpg
Your bitmap path is wrong, you are appending the path to sdcard twice. Try this:
Bitmap myBitmap = BitmapFactory.decodeFile("/mnt/sdcard/447650.jpg");
OR
Bitmap myBitmap = BitmapFactory.decodeFile(picturePath);
OR
Make picturePath the path relative to the path of sdcard.
Related
I see that is a lot of solutions related to this issue, but none of this solved my problem.
I try to read image from png file to bitmap (image is very small ~16px so there is no option that I got OutOfMemoryException) using BitmapFactory.decodeFile(path) function.
My file hierarchy is as simple as possible:
-java
--example.com.app
---MainActivity.class
-res
--drawable
---array.png
I launch application from MainActivity and on button click I try to read png image to bitmap.
I try to read image to bitmap in the following ways:
String path = Uri.parse("android.resource://"+R.class.getPackage().getName() +"/drawable/arrow.png").toString();
Bitmap bitmap = BitmapFactory.decodeFile(path);
String path = Uri.parse("android.resource://drawable/arrow.png").toString();
Bitmap bitmap = BitmapFactory.decodeFile(path);
Everytime bitmap is null. Do you know where could be a problem?
Android API 27
Or use the simple method of BitmapFactory
BitmapFactory.decodeResource(getResources(), R.id.arrow);
Or you can use context.getDrawable(R.drawable.arrow) which will return a drawable that is ready to use.
Read more about this here
If you would like to still use the path then use java.nio.file.Paths(folder1, folder2.. .. .)
Read more about this here
You should use decodeResource, below is an example:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.arrow, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;
Or use its simple way decodeResource.
BitmapFactory.decodeResource(getResources(), R.id.arrow);
I use the following code to get a bitmap from the resources folder put it into a drawable and paint it to the canvas
Drawable f = getResources().getDrawable(R.drawable.harris1);
f.setBounds(a,120,a+200,270);
f.draw(canvas);
I want to be able to get the bitmap from my pictures directory on my device and paint it on the canvas
I get the filepath as follows
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String name = "harris1.jpg";
File file = new File(path, name );
String fileName = String.format("%s/" + name, path);
in this instance filename is equal to "/storage/emulated/0/Pictures/harris1.jpg"
How do i change the line
Drawable f = getResources().getDrawable(R.drawable.harris);
to use the filname of the picture on the device?
Any Help Appreciated
Mark
How about something like:
Bitmap bitmap = BitmapFactory.decodeFile(pathToFile);
canvas.drawBitmap(bitmap, ...);
Use BitmapFactory there's an example you can follow
Reading an image file into bitmap from sdcard, why am I getting a NullPointerException?
You might like this bit of code:
ImageView myImageView = (ImageView)findViewById(R.id.imageview);
BitmapDrawable imagePath = new BitmapDrawable(getResources(), "/storage/emulated/0/Pictures/harris1.jpg");
myImageView.setImageBitmap(imagePath.getBitmap());
I want to set a BitMap from resources but the name of the image came in a string.
How can i set it. My code now is this.
String Nombre= results.getString(0);
RelativeLayout oneLayout = (RelativeLayout)findViewById(R.id.relative1);
ImageView imageView = new ImageView(getApplicationContext());
Bitmap bMap = BitmapFactory.decodeFile("res/drawable/"+Nombre);
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.Nombre);
imageView.setImageBitmap(bMap);
oneLayout.addView(imageView);
This line is not working i know:
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.Nombre);
I need a form to do it, any help is wellcome
If I understand your question, you have the name of the image and want to get the resid and load the image?
Assuming you have an image with the file name in Drawable:
myimage.jpg
Try this snippet:
String nameOfImage = "myimage";
int resId = context.getResources().getIdentifier(name, "drawable", context.getPackageName());
Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), resId);
the problem as to why "Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.Nombre)" to not working is because the name "Nombre" has a capital letter in it,thats not allowed.Remove it,clean your project and then run it
I want to add some text on top of an image.
I read the image from the sd card and set it to a Bitmap variable.
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
Then I added it to a canvas. The code I used is given below,
Canvas c = new Canvas(myBitmap);
But when I added this line, the app crashed at that point. Why is it and how can i solve this ??
Note : Above mention code lines are inside onActivityResult method.
You app crash because your
BitmapFactory.decodeFile
return a immutable bitmap and public Canvas (Bitmap bitmap) only accept a mutable bitmap.
To solve your problem you must convert your immutable Bitmap into mutable see here the method
If you target only >= API 11 , you can use
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), options);
I'm trying to create a canvas from a PNG file that I have loaded into a bitmap, but it gives an error. Here's the code:
public Bitmap CABINET_Bitmap;
AssetManager assetManager = this.getAssets();
inputStream = assetManager.open("background.png");
CABINET_Bitmap = BitmapFactory.decodeStream(inputStream);
// Next line gives error
Canvas cv = new Canvas(CABINET_Bitmap);
If I create the bitmap, rather than load it in, by doing:
CABINET_Bitmap = Bitmap.createBitmap(480, 640, Config.RGB_565);
Canvas cv = new Canvas(CABINET_Bitmap);
Then the canvas creation works. Any ideas what I'm doing wrong?
The documentation states:
Construct a canvas with the specified bitmap to draw into. The bitmap
must be mutable.
The initial target density of the canvas is the same as the given
bitmap's density.
So what I'm assuming is BitmapFactory.decodeStream() is returning an immutable bitmap while Bitmap.createBitmap() is return a mutable one. Instead, use BitmapFactory.Options and set inMutable to true.
BitmapFactory.Options o = new BitmapFactory.Options();
o.inMutable = true;
CABINET_Bitmap = BitmapFactory.decodeStream(inputStream, o);
Canvas cv = new Canvas(CABINET_Bitmap);
See if that works.