Image display from sd card - android

I am new to Android. In my app, I want to access a particular image from my sd card. But the image is not displayed. I have include WRITE_EXTERNAL_STORAGE request in my manifest.
public class Display extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
final TextView name=(TextView) findViewById(R.id.name);
final TextView phone_no=(TextView) findViewById(R.id.phone_no);
File f= new File("/storage/sdcard0/Download/images.jpeg");
Bitmap bMap = BitmapFactory.decodeFile(f.getAbsolutePath());
imageView.setImageBitmap(bMap);
}
I also tried the following codes, but of no use
File mFichier = new File(Environment.getExternalStorageDirectory(),"/storage/sdcard0/Download/images.jpeg");
if(mFichier.exists())
{
imageView.setImageURI(Uri.fromFile(mFichier));
}
and also this code
Bitmap mBitmap = BitmapFactory.decodeFile("/storage/sdcard0/Download/images.jpeg");
imageView.setImageBitmap(mBitmap);
Please help me as to why my image is not getting displayed..

First of all, you want to load file from external storage like sdcard, you'd better use following code:
public File getDataFolder(Context context) {
File dataDir = null;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
dataDir = new File(Environment.getExternalStorageDirectory(), "myappdata");
if(!dataDir.isDirectory()) {
dataDir.mkdirs();
}
}
if(!dataDir.isDirectory()) {
dataDir = context.getFilesDir();
}
return dataDir;
}
It will return a folder which is named "myappdata" located in your sd-card. After that, if you want to load a image from that folder, you can use following code:
File cacheDir = getDataFolder(this);
File cacheFile = new File(cacheDir, "images.jpeg");
InputStream fileInputStream = new FileInputStream(cacheFile);
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inSampleSize = scale;
bitmapOptions.inJustDecodeBounds = false;
Bitmap wallpaperBitmap = BitmapFactory.decodeStream(fileInputStream, null, bitmapOptions);
ImageView imageView = (ImageView)this.findViewById(R.id.preview);
imageView.setImageBitmap(wallpaperBitmap);
If you still have problem with above code, you can check the full example here:
Android Save And Load Downloading File Locally

Related

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

android BitmapFactory.decodeFile returns null when jpeg is saved with Microsoft Paint

I have some jpeg file stored into application private storage. I'm showing them in an ImageView and everything works properly.
But for some jpeg, with sizes similar to the others, BitmapFactory.decodeFile returns null.
This happens with all jpegs generated by Microsoft Paint: just by taking a jpeg that works well, loading into msPaint and saving it unmodified, to get a not working jpeg.
This is the code (the filepath and filename are properly set because it works for many files):
...
public class MainActivity extends ActionBarActivity {
...
private ImageView myImage;
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myImage = (ImageView) findViewById(R.id.imageView1);
File f=new File(basedir, list.get(imageIndex).getFile());
Bitmap b = BitmapFactory.decodeFile(f.getPath());
if (b != null){
myImage.setImageBitmap(b);
} else {
myImage.setImageResource(R.drawable.default);
}
...
}
...
}
I've also tried FileInputStream, as suggested somewhere else but with the same result:
...
File f=new File(basedir, list.get(imageIndex).getFile());
FileInputStream fis = new FileInputStream(f);
Bitmap b = BitmapFactory.decodeStream(fis);
if (b != null){
myImage.setImageBitmap(b);
} else {
myImage.setImageResource(R.drawable.default);
}
...
Does BitmapFactory have any known limitation? Is there any way to check jpeg characteristics??

How to save image to sdcard when using Fresco?

I am using Fresco to download and display Gifs in my app. I want to save the image to sdcard when click it, but i can't figure out how to do it.
final View view = inflater.inflate(R.layout.fragment_gif_viewer, container, false);
SimpleDraweeView draweeView = (SimpleDraweeView) view.findViewById(R.id.image);
Uri uri = Uri.parse(imageUrl);
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setUri(uri)
.setAutoPlayAnimations(true)
.build();
draweeView.setController(controller);
draweeView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Save the gif to /sdcard/test.gif
}
});
I try to get the bitmap from the SimpleDraweeView following the instruction of How do I save an ImageView as an image?, but it's getDrawingCache() returns null
You can do like this
ImageRequest downloadRequest = ImageRequest.fromUri(uri);
CacheKey cacheKey = DefaultCacheKeyFactory.getInstance().getEncodedCacheKey(downloadRequest);
if (ImagePipelineFactory.getInstance().getMainDiskStorageCache().hasKey(cacheKey)) {
BinaryResource resource = ImagePipelineFactory.getInstance().getMainDiskStorageCache().getResource(cacheKey);
File cacheFile = ((FileBinaryResource) resource).getFile();
FileInputStream fis = new FileInputStream(cacheFile);
ImageFormat imageFormat = ImageFormatChecker.getImageFormat(fis);
switch (imageFormat) {
case GIF:
//copy cacheFile to sdcard
break;
}
}
You can use the image pipeline directly to extract your GIF from disk cache.
Then you can use Java File methods to write it to the file system.

how to display image file save on internal storage in android

i have saved drawable images in internal storage by compressing them in bitmap format. the problem is that i want to retrieve those image files and wants to display them in grid view i am able to list the total no of files but i am not able to display those files in image view. here is my code
Intent i = getIntent();
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.SingleView);
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
File[] imageList = directory.listFiles();
if(imageList == null){
imageList = new File[0];
}
Log.i("My","ImageList Size = "+imageList.length);
imageView.setImageResource(imageAdapter. (....?) );
set Image(From Sd card) to imageview get the path of the image which is in sdcard. and you can assign it by creating Bitmap
String filePath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + File.separator + "your_image_name.png";
Bitmap bmp = BitmapFactory.decodeFile(filePath);
imageView.setImageBitmap(bmp);
Another way to set imageview you can also use third party library also which may have resize and Scaling options too. like ImageLoader

Trying to display images from a specific folder in the Android hardware device

//posense is a directory in the Android hardware device and there are some pictures in the directory
ImageView image1, image2;
File imagedirectory;
File[] imagepool;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image1 = (ImageView)findViewById(R.id.imageView1);
imagedirectory = new File("/posense");
imagepool = imagedirectory.listFiles();
image1.setImageResource(imagepool[1]); //this line is giving me an error
}
How can I solve this problem?
Use this instead:
image1.setImageURI(Uri.fromFile(imagepool[1]));
Documentation to be found here: setImageURI and fromFile.
Also, be aware that imagepool[1] is the second element in the array, not the first.
Try these lines instead of imagedirectory = new File("/posense");:
String path = Environment.getExternalStorageDirectory().getName() + File.separatorChar + "posense";
imagedirectory = new File(path);
image1.setImageURI(Uri.fromFile(imagepool[1]));

Categories

Resources