问题数据存到SD卡 [英] problem to store data into sdcard

查看:146
本文介绍了问题数据存到SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题的图像存储到SD车,有没有显示SD卡文件,我想。
这是code。

 包com.sdcard;进口java.io.FileOutputStream中;
进口的java.io.InputStream;
进口java.io.OutputStream中;
进口的java.net.URL;进口android.app.Activity;
进口android.os.Bundle;
进口android.os.Environment;公共类SdcardActivity延伸活动{
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    尝试{
    网址URL =新的URL(http://www.coolpctips.com/wp-content/uploads/2011/05/top-30-android- games.jpg);    输入的InputStream = url.openStream();
    尝试{
        OutputStream的输出=新的FileOutputStream(Environment.getExternalStorageDirectory()+/顶-30-Android的games.jpg);
        尝试{
            INT aReasonableSize = 10;
            字节[]缓冲区=新的字节[aReasonableSize]
            INT读取动作= 0;
            而((读取动作= input.read(缓冲液,0,buffer.length))GT; = 0){
                output.write(缓冲液,0,buffer.length);
            }
        } {最后
            output.close();
        }
    } {最后
        input.close();
    }
    }赶上(例外五){
        e.printStackTrace();
    }
}}


解决方案

在您的code添加此行:

 赶上(例外五){
    e.printStackTrace();
}

,你会发现一个异常的URL格式不正确。加入你想达到这样我可以写一个更好的答案是什么的详细信息。


在您的评论的异常可能与某种Android中的一个错误有关:
HTTP://$c$c.google.com/p /安卓/问题/细节?ID = 2764 。您可以使用此解决方案尝试:<一个href=\"http://stackoverflow.com/questions/3293659/android-java-net-unknownhostexception-host-is-unresolved-strategy-question/3298141#3298141\">Android的java.net.UnknownHostException:主机是尚未解决的(战略问题)或给IP地址而不是DNS

在这里,你已经测试code:

 公共无效的onCreate(捆绑savedInstanceState){
   super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    尝试{
    网址URL =新的URL(http://www.coolpctips.com/wp-content/uploads/2011/05/top-30-android-games.jpg);
    输入的InputStream = url.openStream();
    尝试{
        OutputStream的输出=新的FileOutputStream(Environment.getExternalStorageDirectory()+/顶-30-Android的games.jpg);
        INT aReasonableSize = 1000;
        字节[]缓冲区=新的字节[aReasonableSize]
        INT读取动作= 0 ;;
        尝试{
            而((读取动作= input.read(缓冲液))大于0){
                output.write(缓冲液,0,读取动作);
            }
        } {最后
            output.close();
        }
    } {最后
        input.close();
    }
    }赶上(例外五){
        e.printStackTrace();
    }
}

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();
    }
}

这篇关于问题数据存到SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆