1
안드로이드 내 장치에 SD 카드에 쓸 수 없습니다하지만 내가 올바르게 작동 에뮬레이터 또는 다른 장치에 그것을 시도 할 때 내가 서버에서 이미지를 다운로드 및 SD 카드에 저장하는 것을 시도하고있다
실패하지만 내를 장치가 아닙니다. 내 장치이 내 코드 은하 미니 GT-S5570입니다 :
public String downloadImage(String imageUrl, String id, String folder) {
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/FBLogoQuiz/" + folder + "/";
Log.d("path", path);
File FBLogoQuiz = new File(path);
FBLogoQuiz.mkdirs();
FBLogoQuiz.mkdir();
String data = "";
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
// create a File object for the parent directory
File FBLogoQuizDirectory = new File(path, id + ".png");
// have the object build the directory structure, if needed.
FBLogoQuizDirectory.createNewFile();
data = String.valueOf(String.format(path + id + ".png"));
FileOutputStream stream = new FileOutputStream(data);
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.PNG, 85, outstream);
byte[] byteArray = outstream.toByteArray();
stream.write(byteArray);
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
Manifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
로그 캣 :
05-24 00:26:10.099: W/System.err(28984): java.io.IOException: No such file or directory
05-24 00:26:10.099: W/System.err(28984): at java.io.File.createNewFileImpl(Native Method)
05-24 00:26:10.109: W/System.err(28984): at java.io.File.createNewFile(File.java:1160)
05-24 00:26:10.109: W/System.err(28984): at com.nileworx.footballlogoquiz.GetUpdatesService.downloadImage(GetUpdatesService.java:229)
05-24 00:26:10.109: W/System.err(28984): at com.nileworx.footballlogoquiz.GetUpdatesService.getUpdates(GetUpdatesService.java:197)
05-24 00:26:10.109: W/System.err(28984): at com.nileworx.footballlogoquiz.GetUpdatesService.onHandleIntent(GetUpdatesService.java:127)
05-24 00:26:10.109: W/System.err(28984): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
05-24 00:26:10.109: W/System.err(28984): at android.os.Handler.dispatchMessage(Handler.java:99)
05-24 00:26:10.109: W/System.err(28984): at android.os.Looper.loop(Looper.java:123)
05-24 00:26:10.109: W/System.err(28984): at android.os.HandlerThread.run(HandlerThread.java:60)
분명히 들릴지 모르지만 '/ FBLogoQuiz /'와 전달할 String의 폴더가 존재합니까? – MJ93
그것은 처음으로 만들어 질 것입니다. 나는 이것이 존재하는지 또는 아닐지를 자동으로 확인하고 그렇지 않다면 생성한다고 생각합니다. –
아니요, 디렉토리를 생성하지 않습니다. 내 대답을 참조하십시오. – MJ93