Android Bitmap on Canvas from External File - android

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

Related

Modify a PNG image in the drawable folder

I have a PNG file in my drawable folder.
I need to modify it merging on it a smaller image.
I use this code for create a new Bitmap
Bitmap bigImage = BitmapFactory.decodeResource(getResources(), R.drawable.i10);
Bitmap smallImage = BitmapFactory.decodeResource(getResources(), R.drawable.i11);
Bitmap result = Bitmap.createBitmap(bigImage.getWidth(), bigImage.getHeight(), bigImage.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(bigImage, 0f, 0f, null);
canvas.drawBitmap(smallImage, 10, 10, null);
That I miss is the final part. Assign the new bitmap (bigImage) to "R.drawable.i10"
Resources are static, you cannot change those files once they are shipped in the app (Apk)...
However you can create/manipulate the existing ones & save it as a new file on Storage but that's a different thing & not related with the question!

Programmatically change background / source for ImageView in android

I am creating an android app where I want to display an image full screen. The image is stored in the internal storage of an android phone and the image to be displayed can vary each time. In the layout XML file for that activity, I added an image view tag as shown below:
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/android" />
What shall I type for the android:src ? Any help & suggestions would be appreciated.
Optional: Remove the line
android:src="#drawable/android"
//This can be kept as a default image in case the file you want does not exist.
from the XML layout. Since the image may vary every time, you need to use
File file = new File("/sdcard/Images/test_image.jpg");
if(file.exists()){
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
ImageView image = (ImageView) findViewById(R.id.imageview1);
image.setImageBitmap(bitmap);
}
try the below code to set Bitmap images from a file stored inside a SD-Card .
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);
}
this works at runtime
You can do this at runtime, Try This:
String pathName = "/sdcard/abc.png";
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeFile(pathName);
BitmapDrawable bd = new BitmapDrawable(res, bitmap);
ImageView view = (ImageView) findViewById(R.id.container);
view.setBackgroundDrawable(bd);

Bitmap always show null?

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.

Why the bitmap from res/raw folder is null?

I try to decode a bitmap(1920x1920) from res/raw folder,which is an png resource.I need the full-scale bitmap. I want to use the BimtapFactory.decodeFileDescriptor rather than BitmapFactory.decodeFile||decodeResource because the higher reliability than others to handle OOM:
While I try this code,The bitmap is NULL!!! But the file is not null. I've been playing hard.
Help Please!
Context mCtx=MainActivity.this;
Bitmap bm = null;
//id is the resId in res/raw
AssetFileDescriptor file =mCtx.getResources().openRawResourceFd(R.raw.skin_default_0);
bm = BitmapFactory.decodeFileDescriptor(file.getFileDescriptor(), null,
options);
InputStream bm = getResources().openRawResource(R.raw.splash);
BufferedInputStream bufferedInputStream = new BufferedInputStream(bm);
Bitmap bmp = BitmapFactory.decodeStream(bufferedInputStream);
int nh = (int) (bmp.getHeight() * (512.0 / bmp.getWidth()));
bmp = Bitmap.createScaledBitmap(bmp, 512, nh, true);
view.setImageBitmap(bmp);
Try this code.
Raw folder - keeping the files as uncompressed.
I dont think so , that the BitmapFactory.decodeFileDescriptor will give u efficient result when comparing with the BitmapFactory.decodeStream, Get the inputStream of the file and decode it. if u think the image is too big and u r using many images like this ur app is going to be very heavy, since the raw folder files willnt be compressed. if u want to scaled bitmap use the snippet code that i have added with my code. will give u a better understanding i thnk.
Thanks...

How to save an image in sdcard after applying RGB color filter in android

Presently I am designing an application based on photo editing. While doing this i encountered with a problem i.e.
I have gone through a tutorial "how to apply RGB color filter for an image" from this link and this tutorial is very helpful and nice.
But the problem is after applying RGB color filter to the image i need to save the changed image in sd card.
I have googled a lot for this but didn't found exact thing.
Many of them sugested to use paint() But im not getting how to use that.
So exactly my problem is "After Applying RBG Coloration to image I need to save that image in SD Card, But I do not found the how to do it" ?
How to Save an Android ImageView to SD Card
You have an ImageView which you've modified via various lighting effects and color filters and now you wish to save the result to the SD card as as a .jpg or .png format image.
Here's how:
Load Bitmap image from View.
Save Bitmap image to SD card.
Example:
Don't forget to test for Exceptions and add the necessary permissions to your manifest!
ImageView imageView = <View to save to SD card>;
Bitmap bitmap = loadBitmapFromView(imageView);
final String pathTxt = Environment.getExternalStorageDirectory();
File storagePath = new File(pathTxt);
File file = new File(storagePath, "filename.jpg");
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
out.flush();
out.close();
private Bitmap loadBitmapFromView(View v) {
final int w = v.getWidth();
final int h = v.getHeight();
final Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
final Canvas c = new Canvas(b);
//v.layout(0, 0, w, h);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
}
Theree are two methods for this...
1.after applying RGB values save those values in a variables and apply that values to the image selected.
2.after applying RGB values take the image from image view and save it

Categories

Resources