关于用于下载图像但无法正常工作的Android应用程序

时间:2016-01-11 20:01:52

标签: java android

我的应用程序是通过单击按钮下载图像。当我运行项目时它没有显示错误。甚至目录和文件夹也在外部存储器中创建,其中应保存图像但下载图像的大小是0.0MB。我找不到原因。

这是我的main_activity.class文件

http://

这是我的DownloadFile.class

public class MainActivity extends AppCompatActivity {

    TextView t1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        t1=(TextView)findViewById(R.id.text1);
        Button button=(Button)findViewById(R.id.click);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startDownload();
            }
        });

    }
    public void startDownload(){
        String url="http://p1.pichost.me/i/42/1651529.jpg";
        new DownloadFile(MainActivity.this).execute(url);
    }
}

这是我的AndroidMenifest.xml文件

public class DownloadFile extends AsyncTask<String,String,String> {
    Context context;
    public DownloadFile(Context context) {
        super();
        this.context=context;
    }

    @Override
    protected String doInBackground(String... params) {

        try {

            URL url = new URL(params[0]);
            HttpURLConnection connection=(HttpURLConnection)url.openConnection();
            connection.setDoOutput(true);
            connection.connect();
            File rootDirectory=new File(
                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"my dream");
            if(!rootDirectory.exists()){
                rootDirectory.mkdirs();
            }
            File file=new File(rootDirectory,"nish_img.png");
            file.createNewFile();
            InputStream input=connection.getInputStream();
            FileOutputStream output=new FileOutputStream(file);
            byte buffer[]=new byte[1024];
            int count=0,total=0;
            while((count=input.read())>0) {
                total += count;
                output.write(buffer, 0, count);
            }
            if(total==0){
                Toast.makeText(context,"not completed",Toast.LENGTH_LONG).show();
            }
            output.flush();
            output.close();
            input.close();
            Intent intent=new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            intent.setData(Uri.fromFile(file));
            context.sendBroadcast(intent);

        }catch (Exception e){}
        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Toast.makeText(context,"on starting",Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        Toast.makeText(context,"completed",Toast.LENGTH_LONG).show();
    }
}

这是我的activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.nishtha.service_exp">


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

</manifest>

任何人都可以对这段代码中的错误做好准备吗?它让我疯狂而且它已被弃用代码什么是下载文件的新方法。请给我一些好的东西来理解这一点。

0 个答案:

没有答案