Today I encounter a strange question. I have a png image "image.png", which is generatied by Java code, and anthor image "a.png". I import them to /sdcard/image/ directory. When I invoke code
Bitmap bit = BitmapFactory.decodeFile("/sdcard/image/image.png");
and
bit = BitmapFactory.decodeFile("/sdcard/image/a.png");
on Android, the first bit is null but the latter is not. I am sure the path "/sdcard/image" is right.
Could you help me???
You can get the path of sdcard from this code:
File extStore = Environment.getExternalStorageDirectory();
Then specify the foldername and file name
"/image/"+"image.jpg"
bit = BitmapFactory.decodeFile(extStore +"/image/a.jpg");
Related
The actual code is very simple like this but doesnt works!!
File f = new File("file://E:/test.jpeg");
Picasso.with(this).load(f).
into(avatar);
File f = new File("file://E:/test.jpeg");
First, Android does not have drive letters, let alone an E: drive. That is not a valid filesystem path to any file on any Android device.
Second, the File constructor takes a filesystem path, not a Uri with a scheme (e.g., file://).
Its important to know that the file you are accessing has to be on your DEVICE and NOT from your computer. There is no such directory as a E: drive on phones.
You have a few options. Store the image online, and load it with Picasso (the easiest).
Picasso.with(getActivity())
.load("http://www.image_url.com/image.png")
.into(avatar);
Or, you can get the file path of an image, and then use that with picasso.
File file = new File("path-to-image/image.png")
Picasso.with(getActivity()).load(file).into(avatar);
How do you get the path-to-image of an image on your device?
You can follow this http://www.limbaniandroid.com/2014/03/how-to-get-absolute-path-when-select.html
I'm very new to Android. I'm working with Xamarin and I have to take a picture with camera and save the picture.
I achieved to take the picture, I have a Bitmap object. Then I save it without error but when I try to find it, there is no file.
There is my code :
Bitmap imgBmp = /* image initialized */
//Save image on folder
var folderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var filePath = System.IO.Path.Combine(folderPath, "image1.png");
var stream = new System.IO.FileStream(filePath, FileMode.Create);
bool isOK = imgBmp.Compress(Bitmap.CompressFormat.Jpeg, 95, stream);
stream.Flush();
stream.Close();
I have no error when execute, isOK is true but when I search for the image.png I'm unable to find the file.
With debugger I saw that the path is : /data/user/0/com.myCompagny.MyAppli/files/image1.png but I can not see that folder.
Can someone help me to find my image1.png ?
Or to change the default folder to something like Pictures\MyApplication\image.png but I don't know how to find default folder for images.
Your file is there, but you have no permissions to see it. App directories are only readable to the owning app's uid. If you try to find your file through the shell, your uid is different.
You should try to use another folder path if you want to save file in a world readable location.
I'm just guessing but maybe System.Environment.SpecialFolder.CommonPictures would do.
Hope the subject line makes it clear.
I want to get hold of the file path of the xml file which i store under the res/xml folder.
I'm aware of the InputStream approach by directly reading using the snippet.
... getResources().getXml(R.raw.testxml);
but my requirement is to just get the path of the stored file for the moment.
Any help appreciated
VATSAG
try as to get full path of file stored in res/raw folder
String fullpath = "android.resource://" +
YOUR_FULL_PACKAGE_NAME +
"/"+R.raw.testxml;
hi i have a problem with a bitmap and a picture located here:
file:///mnt/sdcard/Android/data/com.dev.app/files/Pictures/20120924-092226.jpg
so i have a string with this url (the the file exists!)
now i need to get a bitmap, so i do:
String str = "file:///mnt/sdcard/Android/data/com.dev.app/files/Pictures/20120924-092226.jpg";
Bitmap b = BitmapFactory.decodeFile(str);
but b is always null. and i dont know why.
EDIT: i create the file before with a code like this:
String filename = DateFormat.format("yyyyMMdd-hhmmss",Calendar.getInstance().getTime()) + ".jpg";
Uri imageUri = helper.createImageDestinationUri(null, filename);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, requestCode);
There are two possibilities
The file is of incorrect format, i.e although the extension is .jpg its not a jpeg file.
The file cannot be loaded.
You may have a closer look at the adb logs to see what is going on ... if it does not helps , try the following.
Add the file in your drawable folder, and use it in some Test screen. If the file gets displayed then the file format is correct , so android is not able to load the file. Check your permissions, and actually try and read the file first using FileInputStream or somthing else.
If the file is still not displayed then you can be sure that the format is wrong. In that case just try using a different image file.
Hope it helps...
Actually i know how to open PNG files as bitmaps. But my code doens't works for open JPG files, i dont know why.
I can't find correct examples on SO or google about how to do this.
I need to have a bitmap with the JPG file opened from a dir of the sdcard. For example "sdcard/images/01.jpg"
Thanks
File root = Environment.getExternalStorageDirectory();
ImageView IV = (ImageView) findViewById(R.id."image view");
Bitmap bMap = BitmapFactory.decodeFile(root+"/images/01.jpg");
IV.setImageBitmap(bMap);
Always try to use Environment.getExternalStorageDirectory(); instead of sdcard.
You need an ImageView somewhere in your layout, however that's how I do this kind of things.
I use this code personally too, and it works here.
Any of the BitmapFactory.decode* methods should be able to handle standard JPG files.
If you post some code it could be easier to see why it won't work.