1

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();
    }
}}
  • Do you have a permission in your manifest file to write to the SD card? Also remove the exception catching for now, so you can read the error in Logcat. The permission: – Daniel Novak Jun 28 '11 at 12:17
  • You should try to add a bit more information. What fails? What error message do you get? Do you not get an error, but it just doesn't work as expected? I won't be answering the question (because I'm not familiar with this stuff), but try to include as many helpful details as you can. – Alex Jun 28 '11 at 12:17
  • i add this android:name="android.permission.WRITE_EXTERNAL_STORAGE" into manifest but, still not get proper –  Jun 28 '11 at 12:23

3 Answers3

2

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();
    }
}
Community
  • 1
  • 1
bart
  • 2,378
  • 2
  • 19
  • 21
  • 06-28 12:14:23.995: WARN/System.err(2636): java.net.MalformedURLException: Unknown protocol: c –  Jun 28 '11 at 12:26
  • 1
    Eventually, the code connected with writing to sdcard in never executed. From where do you want to take the image? C://Documents and Settings//amitp//Desktop//Winter.jpg indicates that it is from a computer... – bart Jun 28 '11 at 12:28
  • 1
    You cannot do it in this way because the mobile phone does not have access to files stored in the computer. You must put the image somewhere in the network so the phone can download it (for example on http server). – bart Jun 28 '11 at 12:35
  • ok i use http server, but still not get.see my new updated code –  Jun 28 '11 at 12:46
  • there is error like 06-28 12:33:39.005: WARN/System.err(3881): java.net.UnknownHostException: www.coolpctips.com –  Jun 28 '11 at 12:48
  • i have try it but i got this error 06-29 10:37:28.207: WARN/System.err(384): java.io.FileNotFoundException: /mnt/sdcard/top-30-android-games.jpg (Permission denied) –  Jun 29 '11 at 11:20
  • 1
    you should have specified the following permission inside Manifest.xml: – bart Jun 29 '11 at 11:25
0

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

OutputStream output = new FileOutputStream (Environment.getExternalStorageDirectory()+"/myImage.png");
Sujit
  • 10,512
  • 9
  • 40
  • 45
0

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

yep
  • 1,263
  • 1
  • 9
  • 13