单击按钮时图像未保存在设备中 [英] Image not save in the device when click the button

查看:89
本文介绍了单击按钮时图像未保存在设备中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在使用Picasso库下载图像并将其按入按钮时保存在设备中。问题是当我按下按钮时图像未下载并且仅显示消息图像已下载,那么我该如何解决? 这是我的代码

Currently, I am using the Picasso library to download images and save it in the device when I press the button. the problem is when I press the button the image not download and just shown the message "Image Downloaded" , so how can i fix it? Here is my code

PicassoDisplayImageAdapter.java

/*
* This class for display the image when clicking on it
* It gets the data from the class have the images "Images in ArrayList"
* Also It is for download images
*/
public class PicassoDisplayImageAdapter extends AppCompatActivity {

public static final int PERMISSION_WRITE = 0;
String fileUri;
Button download_image;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_image);

/* Display the data in the ImageView with Picasso "ImageView that insert in he activity" */
final ImageView imageView = findViewById(R.id.image_display);
final Intent intent = getIntent();
if (intent.hasExtra("imageUrl")){
    String url = intent.getStringExtra("imageUrl");
    Picasso.with(this)
            .load(url)
            .fit() // to resize the image to imageView
            .placeholder(R.drawable.progress_animation)
            .error(R.drawable.error)
            .into(imageView);
}

/* button to download the image */
download_image = findViewById(R.id.button_download);
checkPermission();
download_image.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (checkPermission()) {
            String URL = intent.getStringExtra("imageUrl");
            SaveImage (URL);
        }
    }
});
}

/* method to save image*/
private void SaveImage(String url) {
Picasso.with(getApplicationContext()).load(url).into(new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        try {
            File mydir = new File(Environment.getExternalStorageDirectory() + "/11zon");
            if (!mydir.exists()) {
                mydir.mkdirs();
            }

            fileUri = mydir.getAbsolutePath() + File.separator + System.currentTimeMillis() + ".jpg";
            FileOutputStream outputStream = new FileOutputStream(fileUri);

            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            outputStream.flush();
            outputStream.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
        Toast.makeText(getApplicationContext(), "Image Downloaded", Toast.LENGTH_LONG).show();
    }
    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
      }
  });
}

 /* runtime storage permission */
 public boolean checkPermission() {
int READ_EXTERNAL_PERMISSION = ContextCompat.checkSelfPermission(this, 
 Manifest.permission.READ_EXTERNAL_STORAGE);
  if((READ_EXTERNAL_PERMISSION != PackageManager.PERMISSION_GRANTED)) {
     ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 
  PERMISSION_WRITE);
     return false;
 }
    return true;
 }

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode==PERMISSION_WRITE && grantResults.length > 0 && grantResults[0] == 
 PackageManager.PERMISSION_GRANTED) {
    //do somethings
    }
  }
}

ImagesRamadanActivity.java 有数据

/*
* This Activity for display the ramadan images
* This class has the data of images
*/
 public class ImagesRamadanActivity extends AppCompatActivity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_ramadan_images);

/* ArrayList for RamadanImages */
final String[] RamadanImages = {
        "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg",
        "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg",
        "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg",
        "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg",
        "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg",
        "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg",
        "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg",
        "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg",
        "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885_1280.jpg",
};

/* make new object and find the view "GridView" */
GridView gridView2 = findViewById(R.id.gridview_image_ramadan);
// display all the images from Array on it
gridView2.setAdapter(new PicassoImagesAdapter(ImagesRamadanActivity.this, RamadanImages));

/* display the image when click on it */
// we made a class for this method "the class called PicassoDisplayImageAdapter"
gridView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // get the image
        String image = RamadanImages[position];
        Intent intent = new Intent(ImagesRamadanActivity.this, PicassoDisplayImageAdapter.class);
        intent.putExtra("imageUrl", image);
        ImagesRamadanActivity.this.startActivity(intent);
    }
});

activity_image_display.xml 活动以显示照片并具有下载按钮图像

activity_image_display.xml activity to display the photo and has the button to download the image

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
  android:id="@+id/image_display"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#fff"
  android:layout_centerHorizontal="true"
  android:layout_centerVertical="true" >
</ImageView>

<Button
  android:id="@+id/button_download"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="download the image"
  android:layout_alignParentBottom="true"
  android:layout_marginBottom="0dp" />

</RelativeLayout>


推荐答案

内部,

onCreate(){
       setContentView(..);
       // requestPermission. ask for permission when app starts.
}    

        @Override
        public void onClick(View v) {
            if (checkPermission()) {
                String URL = intent.getStringExtra("imageUrl");
                SaveImage (URL);
            }
        }

//这类,将此块添加到您的

// kind of this, add this block to your existing code.

  private void requestPermission() {
      if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
         Toast.makeText(MainActivity.this, "Write External Storage permission allows us to save files. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
      } else {
         ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
      }
   }

//确保清单已定义了所有权限

// make sure Manifest has all the permission defined

这篇关于单击按钮时图像未保存在设备中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆