(sorry my english is very poor)
I develop an android app.
I want to download/upload images for my user. i use Retrofit and Picasso on my android app and on my local server i use Symfony. when creating user on my app, i store user's info on MYSQL DABASE with apache and i want to store user's profile picture on a directory like C:/wamp/www/My_app/mobil/photo/user_id/. Now my first question is i don't nkow how to send picture from android app to this folder.
And the second problem is i have tried to download some pictures from apache to android app using Picasso library. i use this cod :
Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
user.setPhoto(HelpLoader.getBitmapAsByteArray(bitmap));
Log.e("PICTURE ok", "********************");
}
#Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
Log.e("PICTURE FAIL", e.getMessage());
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
};
String url = "http://192.168.8.100/My_app/mobil/photo/" + user.getId() + "/profil.jpg";
Picasso.get().load(url).into(target);
i get a 403 Error and when i test the url with a browser apache show this error
Forbidden
You don't have permission to access /My_app/mobil/photo/2/profil.jpg"
but when i try simply to access to the folder /My_app/mobil/photo/2/, i don't have this error.
please help me.
i use :
- apache 2.4.23
- Symfony 3
- Retrofit 2 for send user's informations
- Picasso for the pictures
Related
I need to download 20 images from remote server and store in Android folder.
Here my code:
Interface:
#GET
Call<ResponseBody> downloadFile(#Url String url);
In fragment:
private void downloadImagesList(List<Image> imagesList) {
for (Image image : imagesList) {
final String imageSourceUrl = imageSource.getUrl();
Call<ResponseBody> call = RestClientFactory.getRestClient().downloadFile(imageSourceUrl);
call.enqueue(new DefaultRestClientCallback<ResponseBody>() {
#Override
public void onSuccess(Response<ResponseBody> response) {
Toast.makeText(context, "Success download from url: " + imageSourceUrl, Toast.LENGTH_LONG).show();
// code to write downloaded image to Android's local folder
}
});
}
}
As you can see I iterate loop and call call.enqueue() for every image.
Is it good solution? Is this can perform my android app?
You can use dependencies like Glide , Pissaco etc for downloading images from remote server. Because these provide better caching and error handling. Have a look at the code below. It is using Pissaco to load the image
First add the Pissaco dependency in your build.gradle
compile 'com.squareup.picasso:picasso:2.5.2'
Picasso.with(context)
.load(url)
.resize(50, 50)
.centerCrop()
.into(imageView)
I am using https://github.com/koush/ion in my app to download files from urls and write them to Android devices.
Most of the time it works great but from the statistics there are around 15% of my users getting null file from the call back (onCompleted)
This is part of the code I use.
File d = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File f = new File(d, 'some_file_name');
Ion.with(this).load('some_url')
.progress(new ProgressCallback() {
............
})
.write(f)
.setCallback(new FutureCallback<File>() {
#Override
public void onCompleted(Exception e, File file) {
....... file is null .....
}
});
In my case I had forgotten to check the Android M permissions, the manifest permission were not enough.
I am using Picasso for retrieving and showing images in my Android app. To avoid downloading all images over the network I am trying to add some images with the apk file, as sort of a pre-cached set of images. These images are stored in the assets folder and then copied to the Picasso cache folder on installation. This works as expected, but Picasso still download all images through the network and caches them as .0 and .1 files like this:
root#generic_x86:/data/data/com.my.app/files/images_cache #
ls
10.JPG
100.JPG
101.JPG
102.JPG
11.JPG
1f94664dec9a8c205b7dc50f8a6f3b79.0
1f94664dec9a8c205b7dc50f8a6f3b79.1
2.JPG
4621206beccad87a0fc01df2d080c644.0
4621206beccad87a0fc01df2d080c644.1
The *.JPG images are the ones I copied and the others are the Picasso cached images. Is there a way to make Picasso cache these images properly on installation?
If not, are there any other similar libraries that supports this kind of pre-caching?
Update: trying to cache from Assets folder
I tried making a small snippet that is run at first run of the app. The idea is to iterate the files in the given assets folder and fetch those images with Picasso. However, the below does not cache anything, although I end up in the onSuccess() method of the callback. The asset file names are correct. This is also verified by using the wrong folder name, which puts me in the onError() method of the callback.
I also tried loading it into a temporary ImageView, but it did do any difference.
public static boolean cacheImagesFromAssetsFolder(Context context)
{
boolean ok = false;
try
{
String[] images = context.getAssets().list("my_images");
for (String image : images)
{
Picasso.with(context).load("file:///android_asset/my_images/" + image).fetch(new Callback()
{
#Override
public void onSuccess()
{
// This is where I end up. Success, but nothing happens.
}
#Override
public void onError()
{
}
});
}
ok = true;
}
catch (Exception e)
{
e.printStackTrace();
}
return ok;
}
You could use File URI to request the Picasso to pick the image from your asset location instead of n/w.
Picasso.with(activity) //
.load(Uri.fromFile(file)) // Location of the image from asset folder
Update: How to use your own cache
import com.squareup.picasso.LruCache;
import com.squareup.picasso.Util;
LruCache imageCache = new LruCache(context);
Request request = Request.Builder(Uri.fromFile(asset_file), 0, null).build();
String cacheKey = Util.createKey(request, new StringBuilder());
imageCache.set(cacheKey, bitmap_object_of_asset_image);
Picasso.Builder(context)
.memoryCache(imageCache)
.build().load(asset_url).fetch(callback);
I am trying to load image from my S3 Bucket in my android application. My images are private so I won't be having any specific link for each image.
I'm using link generator,
s3Client.generatePresignedUrl(Constants.S3_BUCKET_NAME, key, expiration);
It generates a URL with let's say 1 hour or 2 min expiration.
Now I have problem in loading the url. I tried loading it by using picasso ,
Picasso.with(context).load(url.toString()).resize(30,38).into(holder.photo);
but it's not quite seems to be working. When I tried that link on browser I got following error
<Code>NoSuchKey</Code>
<Message>The specified key does not exist.</Message>
Try to attach error listener to Picasso and see what's going on. Also read logcat. Print URL which you pass to Picasso, does it correct?
Picasso.Builder builder = new Picasso.Builder(getApplicationContext());
builder.listener(new Picasso.Listener() {
#Override
public void onImageLoadFailed(Picasso arg0, String err) {
Log.e("Picasso Error", "Errored " + err);
}
});
builder.loggingEnabled(true);
Picasso pic = builder.build();
pic.load("image.jpg").into(iv);
I am using Ion library to upload image to web server.But when ever i try to set the parameters using setMultipartParamter i am getting null values.Why this is happening.
Code
Ion.with(getActivity()).load("http://.......").setMultipartParameter("IUser_ID", "126").setMultipartParameter("&User_ID", "amody#gmail.com").setMultipartParameter("&FileTitle", strFileTitle).setMultipartParameter("&DT", strDocumentType).setMultipartFile("", new File(strFilePath)).asString().setCallback(new FutureCallback<String>() {
#Override
public void onCompleted(Exception e, String result) {
Log.e("Upload file response", "" + result);
}
});
I chaecked the server, the file is uploaded properly but the paramters are not.
You are passing in parameter names like they're query strings.
.setMultipartParameter("&User_ID", "amody#gmail.com")
You don't need the ampersand. Your server is likely looking at the wrong keys.