2017-08-29 157 views
0

一次工作我上传图像firebase存储在我的主要活动,并显示在我的recyclerview图像。点击我的recycler视图中的图片后,我将移动到fullimageactivity并传递该图片的url。 下面是其中有一个按钮,并在它的点击,我下载的图像用户的电话下载图片从firebase只能在android

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()); 

    } 

} 

当我点击下载按钮第一次,图像被下载properly.When我回去fullimageactivity代码到我的mainactivity和选择不同的图像,然后点击它,我移动到fullimageactivity,点击按钮fullimageactivity图像不会下载。 总之,它只下载第一张图片,然后它不会下载任何图片。请帮我解决我在做什么错误?

回答

1

如果你的设备包含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(); 

      } 
+0

其工作非常感谢 – Pritish