我正在将图片上传到我的主要活动中的firebase
存储空间,并在我的recyclerview
中显示该图片。点击我的recycler
视图中的图片后,我将移至fullimageactivity
并传递该图片的url
。
以下是fullimageactivity
的代码,它有一个按钮,点击它,我将图像下载到用户的手机
public class FullImageActivity extends AppCompatActivity {
private ImageView imageView;
Button button;
private static final int WATER_REMINDER_NOTIFICATION_ID = 1138;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_image);
imageView = (ImageView) findViewById(R.id.fullimageactivityimageview);
button = (Button) findViewById(R.id.button);
String image = getIntent().getStringExtra("Image");
Glide.with(getApplicationContext()).load(image).into(imageView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
&& checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
) {
requestPermissions(new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
}
} else {
downloadImage();
}
}});
}
private class DownloadImage extends AsyncTask<Object, Object, Bitmap> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Bitmap doInBackground(Object... URL) {
Bitmap bitmapimage = null;
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
File file = new File(myDir, fname);
if (file.exists()) file.delete();
try {
bitmapimage = Glide.with(getApplicationContext()).load(URL[0]).asBitmap().into(100, 100).get();
FileOutputStream out = new FileOutputStream(file);
bitmapimage.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return bitmapimage;
}
@Override
protected void onPostExecute(Bitmap result) {
}
}
private PendingIntent contentIntent(Context context) {
// Intent startActivityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getIntent().getStringExtra("Image")));
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/internal/images/media"));
PendingIntent contentIntent =
PendingIntent.getActivity(context.getApplicationContext(),
id,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
return contentIntent;
}
private Bitmap largeIcon(Context context) {
Resources res = context.getResources();
Bitmap largeIcon = BitmapFactory.decodeResource(res, R.mipmap.notification);
return largeIcon;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 0) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
downloadImage();
}
}
}
public void downloadImage() {
Toast.makeText(getApplicationContext(), "Hi", Toast.LENGTH_SHORT).show();
new DownloadImage().execute(getIntent().getStringExtra("Image"));
android.support.v4.app.NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setSmallIcon(R.mipmap.notification)
.setLargeIcon(largeIcon(getApplicationContext()))
.setContentTitle(getString(R.string.imagedownloaded))
.setContentIntent(contentIntent(getApplicationContext()))
.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
}
NotificationManager notificationManager = (NotificationManager)
getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(WATER_REMINDER_NOTIFICATION_ID, notificationBuilder.build());
}
}
当我第一次点击下载按钮时,图像被正确下载。当我回到我的mainactivity
并选择不同的图像并点击它时,我移动到fullimageactivity
并点击按钮fullimageactivity
图片无法下载。
简而言之,它只下载第一张图片,然后它不再下载任何更多图片。请帮我解决我做错的事情?
答案 0 :(得分:0)
如果您的设备包含marshmello,则会出现此问题,因为在您第一次请求权限时,但是一旦授予权限,则第二次downloadimage()不会被调用,因此请编辑您的代码,如下所示:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
&& checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
) {
requestPermissions(new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
}else {
downloadImage();
} else {
downloadImage();
}