Im having a strange kind of problem, Im uploading an image to a folder and saving the name(the id of the user) and the extension in the database and its working fine but when i change the image, its changing in the folder but the image in the phone not changing though i am editing my SharedPrefManger which save the image name and the extension.
here is how Im changing the image in the folder and its working fine
String uploadId = UUID.randomUUID().toString();
String path = getPath(filepath);
try {
new MultipartUploadRequest(this,uploadId,Constants.URL_UPLOADPIC)
.addFileToUpload(path,"image")
.addParameter("id",SharedPrefManager.getInstance(this).getKeyUserId())
.setNotificationConfig(new UploadNotificationConfig())
.setMaxRetries(2)
.startUpload();
String extention = path.substring(path.lastIndexOf("."));
String id = SharedPrefManager.getInstance(getApplicationContext()).getKeyUserId();
SharedPrefManager.getInstance(getApplicationContext()).uploadpicmanager(id+extention);
Toast.makeText(Profile.this, path, Toast.LENGTH_LONG).show();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Here is the the uploadpicman which change the image name
public boolean uploadpicmanager(String id){
SharedPreferences sharedPreferences3 = mCtx.getSharedPreferences(SHARED_PREF_NAME,Context.MODE_PRIVATE);
SharedPreferences.Editor editor3 = sharedPreferences3.edit();
editor3.putString(KEY_IMAGE,id);
editor3.apply();
return true;
}
Here is how im reading the image and its working fine for the first upload but when i change the pic it doesn't change. Im wondering from where its getting the image if it is not in the folder anymore??
Picasso.with(getApplicationContext())
.load("http://hello.000webhostapp.com/Shop&Go/customers/"+
SharedPrefManager.getInstance(getApplicationContext()).getKeyImage())
.into(profilepic);
Probably Picasso load a cached version of the image.
Try to add
.memoryPolicy(MemoryPolicy.NO_CACHE)
on the builder
Of course this is not a good practise, you should change the path name of the image (and remove the memory policy).
Related
My app can download an image from a raspberry. it works fine. This is the code
public void downloadFile() {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect("******");
ftpClient.login("****","*****");
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
String remoteFile1;
File downloadFile1 = new File(filePath);
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
outputStream1.close();
if (success) {
System.out.println("File #1 has been downloaded successfully.");
} else {
System.out.println("Error in downloading file !");
}
boolean logout = ftpClient.logout();
if (logout) {
System.out.println("Connection close...");
}
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
ftpClient.disconnect();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
And then I can display it so the user of my app can see it. For the image loading, Im using this code and it works too.
private void loadImage(String imagePath) {
Uri imageUri;
String fullImagePath;
Drawable image;
ImageView imageDisplay;
imageUri = Uri.parse(imagePath);
fullImagePath = imageUri.getPath();
image = Drawable.createFromPath(fullImagePath);
imageDisplay=(ImageView) findViewById(R.id.imageDisplay);
imageDisplay.setImageDrawable(image);
}
Now I want to display the image without downloading it in my gallery. But I can't figure out how to do this.
Can someone help me please.
You cannot show an image without download it. Actually when you see something "remotely", you are downloading it.
If you mean that the image is too large and you don't want to download, but want a mechanism for the user can view it. One possible solution is make a thumbnail (reduced image) in server side and show that "preview" to the user. Then if the user want to download it to the gallery you could get the original image.
If you want to display an image without downloading it, it has to be uploaded in a image hosting site or alike so you will just use the link instead of the whole FTP Client.
Basically, you are using a code that is intended for saving an image. And the one you are using for loading the images fetches data from the Drawable. So you are in the wrong path.
I'm working on a school android project.
I need to have a download button which downloads a picture(when we have class)
And after display it in another activity(even in offline mode, and after quiting)
I've tried picasso, but I can't get it to save and use it in offline mode.
For you to support offline mode, You need to Save the image on your disk because when your cache is cleared, The image is cleared as well.
You can easily use Glide to Solve this, also storing on device and retrieving
You can Learn more about Glide here http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en
/** Download the image using Glide **/
Bitmap theBitmap = null;
theBitmap = Glide.
with(YourActivity.this).
load("Url of your image").
asBitmap().
into(-1, -1).
get();
saveToInternalStorage(theBitmap, getApplicationContext(), "your preferred image name");
/** Save it on your device **/
public String saveToInternalStorage(Bitmap bitmapImage, Context context, String name){
ContextWrapper cw = new ContextWrapper(context);
// path to /data/data/yourapp/app_data/imageDir
String name_="foldername"; //Folder name in device android/data/
File directory = cw.getDir(name_, Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,name);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.e("absolutepath ", directory.getAbsolutePath());
return directory.getAbsolutePath();
}
/** Method to retrieve image from your device **/
public Bitmap loadImageFromStorage(String path, String name)
{
Bitmap b;
String name_="foldername";
try {
File f=new File(path, name_);
b = BitmapFactory.decodeStream(new FileInputStream(f));
return b;
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
return null;
}
/** Retrieve your image from device and set to imageview **/
//Provide your image path and name of the image your previously used.
Bitmap b= loadImageFromStorage(String path, String name)
ImageView img=(ImageView)findViewById(R.id.your_image_id);
img.setImageBitmap(b);
Thanks to #Droidman :
How to download and save an image in Android
Of course you can perform downloading and managing images by yourself,
but if your project is quite complex already, there are a lot of
libraries around and you do not need to reinvent the wheel. I won't
post code this time since there are a lot of examples, but I'm going
to tell you about 2 most useful libraries (IMO) related to image
downloading.
1) Android Volley. A powerful networking library created by Google and
covered by official documentation. POST'ing or GET'ing data, images,
JSON - volley will manage it for you. Using volley just for image
downloading is a bit of an overkill in my opinion.
2) Picasso
Image downloading and caching, perfect for
ListView/GridView/RecyclerView. Apache 2.0 license.
3) Fresco
Quite a new image loading library created by Facebook. Progressive
JPEG streaming, gifs and more. Apache 2.0
You could use Android Library called Universal Image Loader:
https://github.com/nostra13/Android-Universal-Image-Loader
I've created a folder 'test' inside res and I want to display them in an image view. How exactly can I fetch the image with a given name in the designated folder?
ImageView test = (ImageView) testing.findViewById(R.id.test);
flag.setImageDrawable(getResources().); <==== This?
EDIT
InputStream is = null;
try {
is = this.getResources().getAssets().open("country_flags/sample.png");
} catch (IOException e) {
;
}
image = (ImageDrawable) BitmapFactory.decodeStream(is);
try {
flag.setImageDrawable(getResources().getAssets().open("country_flags/"+nationality+".png"));
} catch (IOException e) {
e.printStackTrace();
}
How exactly can I fetch the image with a given name in the designated folder?
You don't. You cannot invent new resource types, and so your test directory will, at best, be forever ignored.
I am converting my image to string and storing that string in sharedpreferences. then later on other activity I want to fetch that string convert back to bitmap and display it in image view. Also for precausion if nothing is fetched from sharedpreference I would like to set ic_launcher as default image in my ImageView.
THis is how i am trying to get above task done.
String pic = shared.getString("UserPic","");
Log.i("picstring-verifydetail" , "picstring : "+pic);
if (pic != null && pic != "") {
try {
userpic = ImageHelper.stringToImage(pic);
profilepic.setImageBitmap(userpic);
} catch (IOException e) {
Log.e("picsetting", e.toString());
e.printStackTrace();
}
}
else
{
Bitmap defaultImage = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
profilepic.setImageBitmap(defaultImage);
}
I have also stored some values like name and that are successfully fetched but string for image is not getting fetched from sharedpreferences. It is always going to else part and there again I am getting error : "Source not found" on profilepic.setImageBitmap(defaultImage);. I searched logcat but found no error.
Please help to achieve these 2 task.
Thanks & Regards,
Sourabh Gupta
I don't think what you are trying to do is a good idea.
Try to save the image in SD card or Internal Storage and just store the File path in SharedPreferences.
If you have these images stored in assets OR res folders. You can just store the image names into the SharedPreferences and later you can fetch the image names from it and display on the screen by fetching them from the path.
I want to add an audio file to an image. So when someone gets that image he can extract the sound from it. I think we can add additional data to image header. But I don't know how to do that sort of header processing in Android. Can you please guide me...
Using the below code you can add a small comment to the JPEG header in Android. It's only for JPEG.
final static String EXIF_TAG = "UserComment";
public static boolean setComment(String imagePath, String comment) {
try {
ExifInterface exif = new ExifInterface(imagePath);
exif.setAttribute(EXIF_TAG, comment);
exif.saveAttributes();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
But the problem is you can't put a audio file there because the data stream is way too big to fit in to the header.