问题描述:

i have problem to store image into sd car, there are not display file in sdcard which i want.

this is code.

package com.sdcard;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.URL;

import android.app.Activity;

import android.os.Bundle;

import android.os.Environment;

public class SdcardActivity extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

try{

URL url = new URL ("http://www.coolpctips.com/wp-content/uploads/2011/05/top-30-android- games.jpg");

InputStream input = url.openStream();

try {

OutputStream output = new FileOutputStream (Environment.getExternalStorageDirectory()+"/top-30-android-games.jpg");

try {

int aReasonableSize = 10;

byte[] buffer = new byte[aReasonableSize];

int bytesRead = 0;

while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {

output.write(buffer, 0, buffer.length);

}

} finally {

output.close();

}

} finally {

input.close();

}

}catch (Exception e) {

e.printStackTrace();

}

}}

网友答案:

add this lines in your code:

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

and you will notice an exception that the url is malformed. Add more information about what you want to achieve so that I can write a better answer.


The exception in your comment might be related with some kind of a bug in Android: http://code.google.com/p/android/issues/detail?id=2764. You can try with this solution: Android java.net.UnknownHostException: Host is unresolved (strategy question) or give IP address instead of DNS.

Here you have tested code:

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try{
    URL url = new URL ("http://www.coolpctips.com/wp-content/uploads/2011/05/top-30-android-games.jpg");
    InputStream input = url.openStream();
    try {
        OutputStream output = new FileOutputStream  (Environment.getExternalStorageDirectory()+"/top-30-android-games.jpg");
        int aReasonableSize = 1000;
        byte[] buffer = new byte[aReasonableSize];
        int bytesRead = 0;;
        try {
            while ((bytesRead = input.read(buffer)) > 0) {
                output.write(buffer, 0, bytesRead);
            }
        } finally {
            output.close();
        }
    } finally {
        input.close();
    }
    }catch (Exception e) {
        e.printStackTrace();
    }
}
网友答案:

you should use Environment.getExternalStorageDirectory() instead of mnt/sdcard/

OutputStream output = new FileOutputStream (Environment.getExternalStorageDirectory()+"/myImage.png");
网友答案:

Your problem is you are referencing your host computers local filesystem inside your android device. C:\ isn't a path that android knows how to interpret.

Host it on a local webserver then use something like http://192.168.0.100/your/url/image.png

相关阅读:
Top