简体   繁体   中英

Saving images dynamically from application to SD card

I'm making a gallery in which I load (on run time) images from asset folder. Now I want to save image to SD card on the click event.

For example: When the app starts, the user see images, they can scroll through images and view them (this part is done). The problem is pictures are loading dynamically in my own gallery view. I have not hard coded them.

I want to save it to the SD card. But I don't have the hard coded path of images. There can be any number of images.

 private void CopyAssets() {
        AssetManager assetManager = getAssets();
        InputStream in=null;
    String[] files = null;
        try {
            files = assetManager.list("image");
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        for(String filename : files) {


                try {
                    in = assetManager.open(filename);
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
        try {
                String dirName = Environment.getExternalStorageDirectory().toString(); 
                File newFile = new File(dirName); 
                newFile.mkdirs(); 

        OutputStream out = new FileOutputStream(newFile);
        System.out.println("in tryyyy");

                copyFile(in, out);
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
            } catch(Exception e) {
                Log.e("tag", e.getMessage());
        }

I tried above method, I don't want to copy all the image to the SD card. But only that one which the user chooses from the gallery that too dynamically. As there will be a number of images. Hard coding each image path will be tough.

Is there any way in Android, by which I can get the current image path or URI in a string? What is View v = this.getCurrentFocus(); ? What does it return?

a Gallery extends from AdapterView , and just like on adapterView , you can add a listener for when an item is being selected.

one you know which item was selected , use it in order to copy the image you want to the sd-card.

for better understanding of how to implement the adapter for the adapterView , watch " the world of listView " video . you might want to put the path into the viewHolder (depending on your code and design) .

Here's a method I created that will allow you to save an image (Bitmap) to the memory. The parameters requiere a bitmap object and the filename of that object.

public void writeBitmapToMemory(String filename, Bitmap bitmap) {
        FileOutputStream fos;
        // Use the compress method on the Bitmap object to write image to the OutputStream
        try {
            fos = this.openFileOutput(filename, Context.MODE_PRIVATE);
            // Writing the bitmap to the output stream
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.close();

        } 
        catch (FileNotFoundException e) {
            e.printStackTrace();


        } 
        catch (IOException e) {
            e.printStackTrace();


        }

    }

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM