1

in my project my image are getting downloaded perfectly now I only wanted to know how should I display that downloaded images in "view downloads" activity which is present in navigation drawer...please anybody how can I display

download function in my activity:

downloadimage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DownloadManager.Request request = new DownloadManager.Request(
                    Uri.parse(imgUrl));
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.
                    Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "/my_file.JPEG");
            DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            dm.enqueue(request);

            Toast.makeText(getApplicationContext(), "Downloading File",
                    Toast.LENGTH_LONG).show();
        }
    });

view downloads activity(I haven't done here anything)edit please:

public class NextActivity extends AppCompatActivity {
ImageView myImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_next);
    myImage = (ImageView) findViewById(R.id.down);

    //getActionBar().setDisplayHomeAsUpEnabled(true);
   // getActionBar().setHomeButtonEnabled(true);
    //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    //setSupportActionBar(toolbar);
   // this.getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    File imgFile = new File( Environment.DIRECTORY_DOWNLOADS, "/my_file.JPEG");
    if(!(imgFile==null)){
        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getPath());
        myImage.setImageBitmap(myBitmap);
        Log.d("ashwiniiii","ashwiniii");
    }
    else {
        myImage.setImageResource(R.drawable.ic_launcher_background);
    }


    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    assert navigationView != null;
   //navigationView.setNavigationItemSelectedListener((NavigationView.OnNavigationItemSelectedListener) this);
    if (getSupportActionBar() != null){
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }
   // getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    //getSupportActionBar().setHomeButtonEnabled(true);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        finish(); // close this activity and return to preview activity (if there is any)
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {

        super.onBackPressed();
    }}
Wini
  • 115
  • 14
  • refer this link-https://stackoverflow.com/questions/16557650/how-to-display-images-saved-in-sdcard-folder-in-android – Bunny Nov 25 '19 at 10:23
  • Are you looking at displaying only one image? or list of images in Grid View? – Vinay Jayaram Nov 25 '19 at 10:39
  • list of images in grid view...see I don't want to retrieve the images from sd card..I want whenever user click the download ( image ) that image should display in grid of view download...I hope you get it....vinay – Wini Nov 25 '19 at 10:51
  • Vinay Jayaram do you have solution? – Wini Nov 25 '19 at 11:19

1 Answers1

0

if you are looking for showing images that you have downloaded to your device, the below code should work,

public class NextActivity extends AppCompatActivity {
ImageView img;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_next);
    img=findViewById(R.id.down);
    //getActionBar().setDisplayHomeAsUpEnabled(true);
   // getActionBar().setHomeButtonEnabled(true);
    //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    //setSupportActionBar(toolbar);
   // this.getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    assert navigationView != null;

  //update code
    img.setImageBitmap(getImage());
       //navigationView.setNavigationItemSelectedListener((NavigationView.OnNavigationItemSelectedListener) this);
        if (getSupportActionBar() != null){
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }
       // getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //getSupportActionBar().setHomeButtonEnabled(true);
    }

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        finish(); // close this activity and return to preview activity (if there is any)
    }
    return super.onOptionsItemSelected(item);
}





  public Image getImage(){
    File imgFile = new File("/image_location"); 

//code to get image from path
if(imgFile.exists())


  {
 Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); 
    }

    return myBitmap;

//returns image to specified imageview
        }

@Override
public void onBackPressed() {

        super.onBackPressed();
    }}



  }
Abraham Baby
  • 104
  • 6
  • see I don't want to retrieve the images from sd card..I want whenever user click the download ( image ) that image should display in grid of view download – Wini Nov 25 '19 at 10:52
  • yes,you have downloaded the image from online and it is now stored in your storage directory,so now you can show the image by loading the image uri in to the imageview by, File imgFile = new File("/sdcard/Images/test_image.jpg"); if(imgFile.exists()){ Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); ImageView myImage = (ImageView) findViewById(R.id.imageviewTest); myImage.setImageBitmap(myBitmap); } – Abraham Baby Nov 25 '19 at 11:08
  • Abraham I added your it doesnt displayed ....see my code above I have changed as per your code – Wini Nov 25 '19 at 11:34
  • error at return myBitmap...and img.setImageBitmap(myBitmap); error myBitmap – Wini Nov 25 '19 at 11:46
  • getImage is never used – Wini Nov 25 '19 at 11:49
  • I edited my code as per your code see about public Bitmap getImage() see this part – Wini Nov 25 '19 at 12:08
  • File imgFile = new File(Environment.DIRECTORY_DOWNLOADS, "/my_file.JPEG"); if(imgFile.exists()){ Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); myImage.setImageBitmap(myBitmap); } else { myImage.setImageResource(R.drawable.ic_launcher_background); }I did but still doesnt work – Wini Nov 25 '19 at 12:55
  • E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: Download/my_file.JPEG (No such file or directory) coming – Wini Nov 25 '19 at 14:33