How to get Bitmap from SimpleDraweeView? - android

I want to get Bitmap from SimpleDraweeView of Fresco lib, and save it to SD Card.
SimpleDraweeView is child class of ImageView so it should support getDrawable() but calling this method throws ClassCastException
BitmapDrawable bitmapDrawable = (BitmapDrawable) viewHolder.drawee.getDrawable();
ClassCastException: com.facebook.drawee.generic.RootDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
What is the wayout for this?

s1rius's answer mentioned by Morrison helped me. Here's the code that I used.
ImageRequest downloadRequest = ImageRequest.fromUri(uri);
CacheKey cacheKey = DefaultCacheKeyFactory.getInstance().getEncodedCacheKey(downloadRequest, context);
if (ImagePipelineFactory.getInstance().getMainFileCache().hasKey(cacheKey)) {
BinaryResource resource = ImagePipelineFactory.getInstance().getMainFileCache().getResource(cacheKey);
File cacheFile = ((FileBinaryResource) resource).getFile();
File savedImageFile = new File(Constants.APP_PATH_SAVED_QUOTES, "M1iS1" + "_" + currentPosition + ".jpg");
}
Then I copied cacheFile into savedImageFile.

Related

Get image from drawable by path

I am trying to get image that inside drawable folder by path
so i try this but not working
String path = "android.resource:///" + BuildConfig.APPLICATION_ID + "/drawable/logo_ataturk";
File imgFile = new File(path);
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView imgLogo = findViewById(R.id.imageView_logo);
imgLogo.setImageBitmap(myBitmap);
}
I found this simplest solution
Uri path = Uri.parse("android.resource://" + BuildConfig.APPLICATION_ID + "/drawable/logo_ataturk");
ImageView imgLogo = findViewById(R.id.imageView_logo);
imgLogo.setImageURI(path);
Resources are not files on the device. They are files on your developer machine, nothing more.
You can replace all of this code with:
ImageView imgLogo = findViewById(R.id.imageView_logo);
imgLogo.setImageResource(R.drawable.logo_ataturk);
Drawable drawable = getResources().getDrawable(getResources()
.getIdentifier($name_of_the_image, "drawable", getPackageName()));
or
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.large_icon);
imgLogo.setImageBitmap(myBitmap);

Print logo on top of recipe android

Hello guys i have the problem that i want to include an image in my project and when the user click the print button the image will be printet followed by other information but i dont get it to pass the image path to the PrintTool
PrintTool.printPhotoWithPath(imagePath, this);
and the first line in printtool are this
public static void printPhotoWithPath(String filePath, Context context) {
// Get the picture based on the path
File mfile = new File(filePath/*path*/);
if (mfile.exists()) {
Bitmap bmp = BitmapFactory.decodeFile(filePath/*path*/);
byte[] command = decodeBitmap(bmp);
printPhoto(command, context);
}else{
Log.e("PrintTools_58mm", "the file isn't exists");
}
}
So my problem is, how can i get the path from my image in drawable folder to the code?
Please remove this line
File mfile = new File(filePath/*path*/);
Provided the image is currently in your drawable directory, you can get it like this:
if (mfile.exists()) {
Bitmap bmp = BitmapFactory.decodeFile(getResources(), R.drawable.<yourDrawableName>);
byte[] command = decodeBitmap(bmp);
printPhoto(command, context);
}else{
Log.e("PrintTools_58mm", "the file isn't exists");
}
NB Replace with the name of your Drawable.
If you require the path of the Bitmap, by default it is
String imageUri = "drawable://" + R.drawable.image;
Refer to this.
i hope this helps you
File file = new File(String.valueOf(R.mipmap.imgeName));
file.getPath();

setImageBitmap gets Syntax Errors

I am trying to take a Bitmap and display it in an ImageView.
ImageView iv = new ImageView(this);
Bitmap bMap = BitmapFactory.decodeFile("/res/drawable/" + imageFileName);
iv.setImageBitmap(bMap);
That's my code for it. I create an ImageView and a Bitmap. I want to display my Bitmap in my ImageView. But I always get these two errors on the iv.setImageBitmap(bMap); statment
Syntax error on token "bMap", VariableDeclaratorId expected after this token
Syntax error on token(s), misplaced construct(s)
Does anybody has an idea why this happens and what i have to change?
In OP's pastebin code I've found the following snippet:
String imageFileName =getIntent().getStringExtra("imageFileName");
//String bildquelle = "R.drawable." + imageFileName;
public class ImageChange extends Activity {
ImageView iv = new ImageView(this);
String bildquelle = "R.drawable." + imageFileName;
int id = getResources().getIdentifier(bildquelle, "drawable", getPackageName());
Bitmap bMap = BitmapFactory.decodeResource(getResources(), id);
//Bitmap bMap = BitmapFactory.decodeFile("/res/drawable/" + imageFileName);
iv.setImageBitmap(bMap);
}
There are a couple of problems with this including:
The code is not inside a method body.
The code is a Activity subclass.
The code improperly calls getResources().getIdentifier(...).
Instead of the above code I suggest MERGING the rest of activity with the following:
public class BildAnzeigen extends ActionBarActivity {
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bild_anzeigen);
iv = (ImageView) findViewById(R.id.my_image_view); // replace the id with the one from layout definition
handleIntent(getIntent());
}
private void handleIntent(Intent intent) {
String imageFileName = intent.getStringExtra("imageFileName");
int id = getResources().getIdentifier(imageFileName, "drawable", null);
Bitmap bMap = BitmapFactory.decodeResource(getResources(), id);
iv.setImageBitmap(bMap);
}
}
Note: This will not work if the images are not compiled within your apk. There's no way to add anything to drawables after building. It just doesn't work that way.

SkImageDecoder:factory returned null

I am trying to get the image from sdcard to display it in an imageview in android. Its showing error as SkImageDecoder:factory returned null in android emulator2.2. Here is my code to retrieve image from SDcard to imageview in android.
File imgFile = new File("/sdcard/wm.png");
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageView1);
myImage.setImageBitmap(myBitmap);
}
Please resolve this issue.
Use Environment.getExternalStorageDirectory() or Environment.getExternalStoragePublicDirectory instead of hard coding the path of the sd card (i.e "/sdcard/wm.png").

Android: Displaying .jpg image from sdcard using ImageView

I am trying to display a .jpg from my sd card into an imageview in my layout. I dont get any errors, but nothing gets displayed. I would appreciate any help. Thanks
EDIT:
layout1 = (LinearLayout) findViewById(R.id.layout1Activity);
String imgFile = Environment.getExternalStorageDirectory() + "/Apple.jpg";
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile);
ImageView myImage = new ImageView(this);
myImage.setImageBitmap(myBitmap);
layout1.addView(myImage);
do NOT access the SD card directly, try accessing it trough Environment. Like this:
String imageDir = Environment.getExternalStorageDirectory()+"/apple.jpg";
and then you can call BitmapFactory:
Bitmap myBitmap = BitmapFactory.decodeFile(imageDir);

Categories

Resources