Sandip Armal Patil
Sandip Armal Patil

Reputation: 5905

way to show image from asset and store it in SD card

I am creating one wallpaper application therefore i put some image in asset folder. I need to show this image one by one on button click and store it in sd card.
What i did: I use ImageView and WebView to show image. First, when i use WebView, i stuck on setting image size because it showing to small and i need to show those image as per device window size.
I use following code but didn't help to adjust image on screen

myWebView.loadUrl("file:///android_asset/image.html");
    WebSettings settings = myWebView.getSettings();
    settings.setUseWideViewPort(true);
    settings.setLoadWithOverviewMode(true);

I also set <src img="someimage.jpg" width=""100%"> but it didn't help me.

Then i use ImageView to show image and able to show image at least in some proper size using following code.

InputStream ims = getAssets().open("31072011234.jpg");
        // load image as Drawable
        Drawable d = Drawable.createFromStream(ims, null);
        // set image to ImageView
        imageView.setImageDrawable(d);

My Question is
Which is the good way to show image on screen imageview or webview?. how to take all picture in array when i don't know name of this images and store it in SD card Give me some hint or reference.
Thanks in advance.

Upvotes: 0

Views: 389

Answers (2)

Pratik
Pratik

Reputation: 1541

You don't need to put your images in assets folder, you can use res/drawable to store your images and access it as resource.

Using below code you can access images from drawable without need to know the names of image files.

Class resources = R.drawable.class;
    Field[] fields = resources.getFields();
    String[] imageName = new String[fields.length];     
    int index = 0;
    for( Field field : fields )
    {
        imageName[index] = field.getName();
        index++;
    }

    int result = getResources().getIdentifier(imageName[10], "drawable", "com.example.name");  

and using below code you can save your images to SD card.

File file = new File(extStorageDirectory, "filename.PNG");
 outStream = new FileOutputStream(file);
 bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
 outStream.flush();
 outStream.close();

Upvotes: 0

Binoy Babu
Binoy Babu

Reputation: 17119

The best way to show an image would be ImageView (That's why it's called an Image View), I recommend that you add the image in res/drawable folder and show the image using:

imageView.setImageResource(R.id.some_image);

The resource can be saved to sdcard using:

Bitmap bm = BitmapFactory.decodeResource( getResources(), R.id.some_image);
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File file = new File(extStorageDirectory, "someimage.PNG");
outStream = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();

Upvotes: 0

Related Questions