Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package it.rapz.sfondideirapper;
- import android.Manifest;
- import android.app.DownloadManager;
- import android.content.BroadcastReceiver;
- import android.content.ContentValues;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.content.pm.PackageManager;
- import android.database.Cursor;
- import android.media.MediaScannerConnection;
- import android.net.Uri;
- import android.os.Environment;
- import android.provider.MediaStore;
- import android.support.v4.app.ActivityCompat;
- import android.support.v4.content.ContextCompat;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ImageView;
- import android.widget.ListView;
- import android.widget.Toast;
- import java.io.File;
- import java.util.Arrays;
- import java.util.Random;
- public class Main2Activity extends AppCompatActivity {
- ListView listview;
- Context context;
- private static LayoutInflater inflater = null;
- private BroadcastReceiver mDLCompleteReceiver;
- private static BroadcastReceiver mDLCompleteReceiver2;
- public static final int idFoto = 14;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main2);
- Context context3 = getApplicationContext();
- CharSequence text = "Premi uno sfondo per scaricarlo!";
- int duration = Toast.LENGTH_LONG;
- Toast toast = Toast.makeText(context3, text, duration);
- toast.show();
- int permissionCheck = ContextCompat.checkSelfPermission(Main2Activity.this,
- Manifest.permission.WRITE_EXTERNAL_STORAGE);
- Log.e("ImageSaver", "WRITE_EXTERNAL_STORAGE:"+permissionCheck);
- if (ContextCompat.checkSelfPermission(Main2Activity.this,
- Manifest.permission.WRITE_EXTERNAL_STORAGE)
- != PackageManager.PERMISSION_GRANTED) {
- if (ActivityCompat.shouldShowRequestPermissionRationale(Main2Activity.this,
- Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
- } else {
- // No explanation needed, we can request the permission.
- ActivityCompat.requestPermissions(Main2Activity.this,
- new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
- 1);
- int permissionCheck2 = ContextCompat.checkSelfPermission(Main2Activity.this,
- Manifest.permission.WRITE_EXTERNAL_STORAGE);
- Log.e("ImageSaver", "WRITE_EXTERNAL_STORAGE:"+permissionCheck2);
- }
- }
- listview = (ListView) findViewById(R.id.listview);
- String[] str = new String[idFoto];
- listview.setAdapter(new yourAdapter(this, str));
- listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- Log.d("TAAAAG", "destra: ");
- Log.d("TAAAAG", "pos:"+position);
- Boolean ok = true;
- ImageView mImageView = (ImageView) findViewById(R.id.immagine);
- String name = "";
- if (name.isEmpty()) name = "sfondo_" + Math.abs(new Random().nextInt());
- int numFoto = idFoto - position;
- String url = "http://rapz.it/Sfondi/"+(numFoto)+".jpg";
- Log.d("NuovoTag", "pos:"+position);
- Log.d("NuovoTag", "id:"+idFoto);
- Log.d("NuovoTag", "http://rapz.it/Sfondi/"+(numFoto)+".jpg");
- DownloadManager.Request request;
- try {
- request = new DownloadManager.Request(Uri.parse(url));
- } catch (IllegalArgumentException e) {
- Log.d("NuovoTag", "Error: " + e.getMessage());
- request = null;
- ok = false;
- }
- /* allow mobile and WiFi downloads */
- if(ok) {
- request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
- request.setTitle("DM Example");
- request.setDescription("Downloading file");
- /* we let the user see the download in a notification */
- request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
- /* Try to determine the file extension from the url. Only allow image types. You
- * can skip this check if you only plan to handle the downloaded file manually and
- * don't care about file managers not recognizing the file as a known type */
- String[] allowedTypes = {"png", "jpg", "jpeg", "gif", "webp"};
- String suffix = url.substring(url.lastIndexOf(".") + 1).toLowerCase();
- if (!Arrays.asList(allowedTypes).contains(suffix)) {
- Log.d("NuovoTag", "Invalid file extension. Allowed types: \n");
- String tvStatus = "";
- ok = false;
- }
- if (ok) {
- /* set the destination path for this download */
- request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS +
- File.separator + "sfondi_dei_rapper", name + "." + suffix);
- /* allow the MediaScanner to scan the downloaded file */
- request.allowScanningByMediaScanner();
- final DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
- /* this is our unique download id */
- final long DL_ID = dm.enqueue(request);
- /* get notified when the download is complete */
- mDLCompleteReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- /* our download */
- if (DL_ID == intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1L)) {
- //tvStatus.clearAnimation();
- /* get the path of the downloaded file */
- DownloadManager.Query query = new DownloadManager.Query();
- query.setFilterById(DL_ID);
- Cursor cursor = dm.query(query);
- if (!cursor.moveToFirst()) {
- //tvStatus.setText("Download error: cursor is empty");
- Log.d("NuovoTag", "IDownload error: cursor is empty");
- return;
- }
- if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))
- != DownloadManager.STATUS_SUCCESSFUL) {
- //tvStatus.setText("Download failed: no success status");
- Log.d("NuovoTag", "Download failed: no success status");
- return;
- }
- String path = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
- Log.d("NuovoTag", "File download complete. Location: \n" + path);
- Context context2 = getApplicationContext();
- CharSequence text = "Sfondo scaricato! \nPuoi trovarlo nella galleria";
- int duration = Toast.LENGTH_LONG;
- Toast toast = Toast.makeText(context2, text, duration);
- toast.show();
- }
- }
- };
- /* register receiver to listen for ACTION_DOWNLOAD_COMPLETE action */
- registerReceiver(mDLCompleteReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
- }
- }
- }
- });
- }
- public void MetodoDownload(int position, int destra)
- {
- Log.d("nTAAAAG", "1");
- Log.d("TAAAAG", "destra: "+destra);
- Log.d("TAAAAG", "pos:"+position);
- Boolean ok = true;
- String name = "";
- if (name.isEmpty()) name = "sfondo_" + Math.abs(new Random().nextInt());
- int numFoto = idFoto - position;
- String url = "";
- if(destra == 1)
- {
- url = "http://rapz.it/Sfondi/"+(numFoto*2)+".jpg";
- }
- else
- {
- url = "http://rapz.it/Sfondi/"+(numFoto)+".jpg";
- }
- Log.d("nTAAAAG", "1");
- //////////////////
- Log.d("NuovoTag", "pos:"+position);
- Log.d("NuovoTag", "id:"+idFoto);
- Log.d("NuovoTag", "http://rapz.it/Sfondi/"+(numFoto)+".jpg");
- DownloadManager.Request request;
- try {
- request = new DownloadManager.Request(Uri.parse(url));
- } catch (IllegalArgumentException e) {
- Log.d("NuovoTag", "Error: " + e.getMessage());
- request = null;
- ok = false;
- }
- /* allow mobile and WiFi downloads */
- if(ok) {
- request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
- request.setTitle("DM Example");
- request.setDescription("Downloading file");
- /* we let the user see the download in a notification */
- request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
- /* Try to determine the file extension from the url. Only allow image types. You
- * can skip this check if you only plan to handle the downloaded file manually and
- * don't care about file managers not recognizing the file as a known type */
- String[] allowedTypes = {"png", "jpg", "jpeg", "gif", "webp"};
- String suffix = url.substring(url.lastIndexOf(".") + 1).toLowerCase();
- if (!Arrays.asList(allowedTypes).contains(suffix)) {
- Log.d("NuovoTag", "Invalid file extension. Allowed types: \n");
- String tvStatus = "";
- ok = false;
- }
- if (ok) {
- /* set the destination path for this download */
- request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS +
- File.separator + "sfondi_dei_rapper", name + "." + suffix);
- /* allow the MediaScanner to scan the downloaded file */
- request.allowScanningByMediaScanner();
- final DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
- /* this is our unique download id */
- final long DL_ID = dm.enqueue(request);
- /* get notified when the download is complete */
- mDLCompleteReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- /* our download */
- if (DL_ID == intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1L)) {
- //tvStatus.clearAnimation();
- /* get the path of the downloaded file */
- DownloadManager.Query query = new DownloadManager.Query();
- query.setFilterById(DL_ID);
- Cursor cursor = dm.query(query);
- if (!cursor.moveToFirst()) {
- //tvStatus.setText("Download error: cursor is empty");
- Log.d("NuovoTag", "IDownload error: cursor is empty");
- return;
- }
- if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))
- != DownloadManager.STATUS_SUCCESSFUL) {
- //tvStatus.setText("Download failed: no success status");
- Log.d("NuovoTag", "Download failed: no success status");
- return;
- }
- String path = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
- Log.d("NuovoTag", "File download complete. Location: \n" + path);
- Context context2 = getApplicationContext();
- CharSequence text = "Sfondo scaricato! \nPuoi trovarlo nella galleria";
- int duration = Toast.LENGTH_LONG;
- Toast toast = Toast.makeText(context2, text, duration);
- toast.show();
- }
- }
- };
- /* register receiver to listen for ACTION_DOWNLOAD_COMPLETE action */
- registerReceiver(mDLCompleteReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
- }
- }
- destra = 0;
- }
- static public boolean resetExternalStorageMedia(Context context) {
- if (Environment.isExternalStorageEmulated())
- return (false);
- Uri uri = Uri.parse("file://" + Environment.getExternalStorageDirectory());
- Intent intent = new Intent(Intent.ACTION_MEDIA_MOUNTED, uri);
- context.sendBroadcast(intent);
- return (true);
- }
- static public void notifyMediaScannerService(Context context, String path) {
- MediaScannerConnection.scanFile(context,
- new String[] { path }, null,
- new MediaScannerConnection.OnScanCompletedListener() {
- public void onScanCompleted(String path, Uri uri) {
- Log.i("ExternalStorage", "Scanned " + path + ":");
- Log.i("ExternalStorage", "-> uri=" + uri);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement