Hi I want to show an image from SD card. I have 10 images. out of which i am able to show 9 images but 1 image i cannot show in image view.
My SD card location is correct. I am using android programming. I am getting file exists as true. also I am getting not null input stream but when I want to get Drawable object for some fiels i am not getting but for all others getting drawable object.
Also I have tried using
iv1.setImageURI(Uri.parse(str1))
but i didnot get any solution
Following is my code snippet
InputStream is1 = getBitMapImage(str1);
InputStream is2 = getBitMapImage(str2);
InputStream is3 = getBitMapImage(str3);
Drawable d1 = Drawable.createFromStream(is1, "first");
Drawable d2 = Drawable.createFromStream(is2, "second");
Drawable d3 = Drawable.createFromStream(is3, "third");
iv1.setImageDrawable(d1);
iv2.setImageDrawable(d2);
iv3.setImageDrawable(d3);
System.out.println(is1+"....d1...."+d1);
System.out.println(is2+"....d2...."+d2);
System.out.println(is3+"....d3...."+d3);
public static BufferedInputStream getBitMapImage(String filePath) {
Log.e("Utilities", "Original path of image from Utilities "+filePath);
File imageFile = null;
FileInputStream fileInputStream = null;
BufferedInputStream buf= null;
try{
imageFile= new File(filePath);
System.out.println("Does Images File exist ..."+imageFile.exists());
fileInputStream = new FileInputStream(filePath);
buf = new BufferedInputStream(fileInputStream);
}catch(Exception ex){
}finally{
try{
imageFile = null;
fileInputStream.reset();
fileInputStream.close();
}catch(Exception ex){}
}
return buf;
}
Some image files cannot be shared. Blackbery rem file cannot be shared. That type of file cannot be opened if stored directly. So during storing convert that file into .jpg file and then only u can open those file. For checking purpose pullout your file from the device to your system then convert in into .jpg file using gimp and push into device once again then try to run your program and check whether your image file is diplayed in your image view.
Thanks
Sunil
Related
I am developing an Android app. In my app, I am uploading multiple images to server using Retrofit network library. Before I uploading file I create a temporary file from bitmaps. Then delete them after uploaded.
photoFiles = new ArrayList<File>();
MultipartBody.Builder requestBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
int index = 0;
for(Bitmap bitmap: previewBitmaps)
{
File file = null;
try{
String fileName = String.valueOf(System.currentTimeMillis())+".jpeg";
file = new File(Environment.getExternalStorageDirectory(), fileName); // create temporary file start from here
if(file.exists())
{
file.delete();
}
OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.close();
photoFiles.add(file);
requestBodyBuilder.addFormDataPart("files",file.getName(), RequestBody.create(MediaType.parse("image/png"),file));
}
catch (Exception e)
{
Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
}
index++;
}
//Upload process goes here and delete files back after upload
Using above code, all working fine. But the problem is I have to create temporary files. I do not want to create temporary files. What I want to do is I create array list of Uri string when I pick up the file. Then on file upload, I will convert them to file back and do the upload process.
photoFiles = new ArrayList<File>();
MultipartBody.Builder requestBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
int index = 0;
for(Bitmap bitmap: previewBitmaps)
{
File file = null;
try{
Uri uri = Uri.parse(photosUriStrings.get(index));
file = new File(getPathFromUri(uri));
Toast.makeText(getBaseContext(),getPathFromUri(uri),Toast.LENGTH_SHORT).show();
photoFiles.add(file);
requestBodyBuilder.addFormDataPart("files",file.getName(), RequestBody.create(MediaType.parse("image/png"),file));
}
catch (Exception e)
{
Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
}
index++;
}
As you can see in the above, I am converting the URI string back to file and then upload it. But this time retrofit unable to upload the file. File is not null as well. So I am pretty sure the error is with converting uri string back to image file back because my old code above working fine. Why can I not do that? How can I successfully convert from URI to image file back please?
I found this
Convert file: Uri to File in Android
and
Create File from Uri type android
both not working.
I am not clear about your question but I think this may help you. This single line code will help you to convert URI to file and show in your view.
Picasso.with(getContext()).load("URI path").into(holder.imgID);
I am currently using nostra image loader for loading images...
in my code i am using like this..
public static final String[] IMAGES = {"http://mywebsite.com/tittle/image1.jpg",
"http://mywebsite.com/tittle/image2.jpg",
"http://mywebsite.com/tiitle/image3.jpg",
"http://mywebsite.com/tittle/image4.jpg",
"http://mywebsite.com/tittle/image5.jpg",....};
Is it possible load images from the directory which has images in it..
like ""http://mywebsite.com/tittle"
You want to download all images from a single directory right ?
File directory= new File("some public directory");
for (File file : directory.listFiles())
{
if (FileNameUtils.getExtension(file.getName()).equals("jpg"))
{
//get file here
}
}
Your question is not clear. Can you elaborate which directory are you taking about. Is it the SD Card directory (external memory) , Phone directory (internal memory) or a directory on the web server.
Still for the answer, you can load images from all the above 3 mentoined memories.
This the process of downloading any image from the web server and saving it to local memory:
public class DownloadImage
{
public DownloadImage(String url,String file) throws IOException
{
File fileName = new File(file);
URL myImageURL = new URL(url);
HttpURLConnection connection = (HttpURLConnection)myImageURL.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
OutputStream fOut = null;
fOut = new FileOutputStream(fileName);
myBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
}
}
Make a new class named DownloadImage and copy the above code. Then in "url" pass the HTTP Url from where the image has to be downloaded and in "file" pass the local memory address where the image has to be stored.
I'm trying to share an image in an app I have made that downloads an Image and writes it to a file. But any time I try to share it, it says can't upload file or just does nothing. It's not coming up in the logcat so I'm kinda stuck for ideas on how to fix it.
The image that is downloaded is displayed in an image view like this
iView.setImageBitmap(im);
String path = ContentFromURL.Storage + "/temp.jpg";
File temp = new File(path);
uri = Uri.fromFile(temp);
iView.setImageURI(uri);
Asynch task to download file
HttpURLConnection connection;
try {
String url = params[0];
connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestProperty("Accept-Charset","UTF-8");
connection.connect();
InputStream input = connection.getInputStream();
image = BitmapFactory.decodeStream(input);
File temp = new File(Storage,"temp.jpg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
FileOutputStream fo = new FileOutputStream(temp);
fo.write(bytes.toByteArray());
fo.close();
String path = temp.getAbsolutePath();
Log.d("Asynch", "image shuould exist");
SharePage.act.runOnUiThread(new Runnable()
{
public void run()
{
SharePage.setImage(image);
}
}
);
creating intent
twitterIntent = new Intent(Intent.ACTION_SEND);
twitterIntent.setClassName("com.twitter.android",packageName);
twitterIntent.setType("image/jpeg");
twitterIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(twitterIntent);
I know that I should use the built in android share thing but its not working either when I try to share the image
The problem was where I was trying to store the Image, I wanted to have it so that the user never saw the image and it was deleted when it wasn't needed anymore but the other apps didn't have access to the directory. So I have since moved it to the external storage directory.
Ok, so far all the answers I've found seem to be answered to by people who do not know the answer...
This should be a simple one (free rep for you yay):
I have a file in res/raw/ called overworld_a.tmx
I need to load it using the path as a string then the filename added to the string, since it varies.
Like so:
String mapName = "overworld_a.tmx";
try {
TMXMapReader mapReader = new TMXMapReader();
map = mapReader.readMap("raw/"+mapName);
} catch (Exception e) {
System.out.println("Error while reading the map:\n" + e.getMessage());
return;
}
Problem is, this obviously doesn't work.... Simple Question is, what is the relative path to that file?
And if this is not a possibility:
Complex question is, how do I open varying files from res folder and its children?
You can read overworld_a.tmx from res/raw dir as :
1. use openRawResource for reading overworld_a.tmx as InputStream from raw folder :
String mapName = "overworld_a"; //<< just pass name without file extension
resID = getResources().getIdentifier(mapName, "raw", getPackageName());
InputStream inputStream = getResources().openRawResource(resID);
2. Get ByteArrayOutputStream from inputStream :
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//... your code for reading byteArray from inputStream
3. Pass byteArrayOutputStream to mapReader.readMap :
TMXMapReader mapReader = new TMXMapReader();
map = mapReader.readMap(byteArrayOutputStream);
//.....
Im my app when the splash screen gets started I am just hitting an URL and getting back an XML file. From that XML file i am parsing out data such as an user name, id and an URL to download an image. From that url i want to download an image and i want to store the image in a particular name in my app itself.I want to use the same image as a background in another activity. How can i download and store the image in my app. Where can it be stored in my app, either in raw folder or in drawable.
Before storing the name how come the image can be set as a background image in the particular activity, Please help me friends
This is the code to download your image from an url :
InputStream in = new URL(image_url).openConnection().getInputStream();
Bitmap bm = BitmapFactory.decodeStream(in);
Note that it should be done asynchronously (like in an asynctask)
Than you can store the Bitmap on the system using:
File fullCacheDir = new File(Environment.getExternalStorageDirectory(),cacheDir);
String fileLocalName = name+".JPEG";
File fileUri = new File(fullCacheDir, fileLocalName);
FileOutputStream outStream = null;
outStream = new FileOutputStream(fileUri);
image.compress(Bitmap.CompressFormat.JPEG, 75, outStream);
outStream.flush();
Note that this is just an example on how to store your image and there is other ways. You should look at documentation anyway.
If you want it for your application. Better download the image save it as Drawable instance and use it in your application where you want
public static Drawable drawable = null;
//get image from URL and store it in Drawable instance
public void getImageFromURL(final String urlString) {
Thread thread = new Thread() {
#Override
public void run() {
//TODO : set imageView to a "pending" image
InputStream is = null;
try{
URLConnection urlConn = new URL(urlString).openConnection();
is= urlConn.getInputStream();
}catch(Exception ex){}
drawable = Drawable.createFromStream(is, "src");
}
};
thread.start();
}
set Background image to any view
void setImage(View myView){
myView.setBackgroundDrawable(drawable);
}