Piccasso WebLink : http://square.github.io/picasso/
Help Web -1 : https://code.tutsplus.com/tutorials/android-sdk-working-with-picasso--cms-22149
Step-1 : Add library to App level Gradle
compile 'com.squareup.picasso:picasso:2.5.2'
Step - 2 : Add permission to AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Step-3 : activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
/>
</LinearLayout>
Step- 4 : MainAcivity.java
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private ImageView mImageView;
private String mURL = "http://www.allindiaflorist.com/imgs/arrangemen4.jpg";
private String urls = "http://api.androidhive.info/images/sample.jpg";
private String[] urlArray = {mURL, urls};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.image);
}
@Override
protected void onResume() {
super.onResume();
Picasso.with(getApplicationContext()).load(mURL).into(mImageView);
for (int i=0; i<urlArray.length; i++) {
Picasso.with(getApplicationContext()).load(urlArray[i]).into(target);
}
}
private Target target = new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {
@Override
public void run() {
File folder = new File(MainActivity.this.getExternalFilesDir(null), "/Picasso/");
if (!folder.exists()) {
if (!folder.mkdir()) {
Log.e("ERROR", "Cannot create a directory!");
} else { folder.mkdir(); }
}
File[] fileName = {new File(folder, "one.jpg"), new File(folder, "two.jpg")};
for (int i=0; i<fileName.length; i++) {
if (!fileName[i].exists()) {
try {
fileName[i].createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
FileOutputStream outputStream = new FileOutputStream(String.valueOf(fileName[i]));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}).start();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
Toast.makeText(getApplicationContext(),"Failed", Toast.LENGTH_LONG).show();
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
Description :
22.
No comments:
Post a Comment