data_SDCard_storage
2015-11-03 20:43
169 查看
MainActivity.java
HttpUtils.java
AsyncTaskService.java
MyTest.java(单元测试)
SdCardService.java
AndroidMainfest,xml
layout_main.xml
package com.example.hd.picture_sdcard; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { private static final String PATH = "http://pic28.nipic.com/20130329/12243865_163418381104_2.jpg"; private Button mButton; private ImageView mImageView; private AsyncTaskService myTask; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButton = (Button) findViewById(R.id.button1); mImageView = (ImageView) findViewById(R.id.imageView1); myTask = new AsyncTaskService(MainActivity.this, mImageView); mButton.setOnClickListener(new ShowImage()); } class ShowImage implements View.OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub myTask.execute(PATH); } } }
HttpUtils.java
package com.example.hd.picture_sdcard; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class HttpUtils { public HttpUtils() { // TODO Auto-generated constructor stub } public byte[] getImage(String path){ byte[] data = null; byte[] data_temp = null; InputStream in = null; ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); int len = 0; try { URL url = new URL(path); HttpURLConnection urlcon = (HttpURLConnection) url.openConnection(); in = new BufferedInputStream(urlcon.getInputStream()); data_temp =new byte[100]; data = new byte[in.available()]; while ((len=in.read(data_temp, 0, 100)) != -1) { swapStream.write(data_temp); } data = swapStream.toByteArray(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(data != null){ try { in.close(); swapStream.close(); return data; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return data; } }
AsyncTaskService.java
package com.example.hd.picture_sdcard; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.widget.ImageView; public class AsyncTaskService extends AsyncTask<String, Void, byte[]>{ private Context context; private ImageView imageview; public AsyncTaskService(Context context,ImageView imageview) { // TODO Auto-generated constructor stub this.context=context; this.imageview = imageview; } @Override protected void onPostExecute(byte[] result) { // TODO Auto-generated method stub super.onPostExecute(result); if(result != null){ Bitmap bitmap = BitmapFactory.decodeByteArray(result, 0, result.length); imageview.setImageBitmap(bitmap); }else{ imageview.setImageResource(R.drawable.ic_launcher); } } @Override protected void onProgressUpdate(Void... values) { // TODO Auto-generated method stub super.onProgressUpdate(values); } @Override protected byte[] doInBackground(String... params) { // TODO Auto-generated method stub HttpUtils http = new HttpUtils(); byte[] data = http.getImage(params[0]); SdCardService sdcard = new SdCardService(); sdcard.saveFileToSd("mypicture.jpg", data); return data; } }
MyTest.java(单元测试)
package com.example.hd.picture_sdcard; import android.test.AndroidTestCase; import android.test.AndroidTestRunner; import android.util.Log; public class MyTest extends AndroidTestCase{ public MyTest() { // TODO Auto-generated constructor stub } public void saveTest(){ SdCardService sdCard = new SdCardService(); sdCard.saveToSd("dfjdsjkad.txt", "fdsfa".getBytes()); } public void deleteTest(){ SdCardService sdcard = new SdCardService(); boolean flag = sdcard.delateFile("txt", "jjjj.txt"); Log.i("deleteTest", flag+""); } }
SdCardService.java
package com.example.hd.picture_sdcard; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.os.Environment; public class SdCardService { public SdCardService() { // TODO Auto-generated constructor stub } public boolean saveToSd(String fileName,byte[] data){ String state = Environment.getExternalStorageState(); File file = Environment.getExternalStorageDirectory(); FileOutputStream outputStream = null; if(state.equals(Environment.MEDIA_MOUNTED)){ try { outputStream = new FileOutputStream(new File(file, fileName)); outputStream.write(data, 0, data.length); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(outputStream != null){ try { outputStream.close(); return true; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } return false; } public boolean saveFileToSd(String fileName,byte[] data){ String state = Environment.getExternalStorageState(); File file = null; FileOutputStream outputStream = null; if(state.equals(Environment.MEDIA_MOUNTED)){ if(fileName.endsWith(".mp3")||fileName.endsWith("rm")){ file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC); }else if(fileName.endsWith(".mp4")||fileName.endsWith("avi")){ file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES); }else if(fileName.endsWith(".jpg")||fileName.endsWith(".png")){ file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); }else{ file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); } } try { outputStream = new FileOutputStream(new File(file, fileName)); outputStream.write(data,0,data.length); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { outputStream.close(); return true; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return false; } public boolean delateFile(String folder,String fileName){ String state = Environment.getExternalStorageState(); File file = null; boolean flag = false; File sdCardroot; if(state.equals(Environment.MEDIA_MOUNTED)){ sdCardroot = Environment.getExternalStorageDirectory(); file = new File(sdCardroot.getAbsoluteFile()+"/"+folder+"/"+fileName); if(file.exists()){ flag = file.delete(); return flag; }else { } } return false; } }
AndroidMainfest,xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.hd.picture_sdcard" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.example.hd.picture_sdcard"></instrumentation> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <uses-library android:name="android.test.runner"/> <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
layout_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.hd.picture_sdcard.MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_marginBottom="127dp" android:layout_marginLeft="106dp" android:text="Button" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="134dp" /> </RelativeLayout>
相关文章推荐
- Python中split分割字符的使用
- LeetCode 014 Longest Common Prefix
- Android API之ImageView.ScaleType代码演示
- [原创]农村孩子要不要上那些“烂大学”?
- const小总结
- Jquery 实现表单提交按钮变灰,防止多次点击提交重复数据
- CentOS 6.7下PXE+Kickstart无人值守安装操作系统
- 好坑啊 NYOJ303 序号互换
- AngularJS API之copy深拷贝
- T-SQL学习之路之数据库完整性之默认值
- va_start和va_end的用法详解
- RTImageAssets 自动生成 AppIcon 和 @2x @1x 比例图片
- 把毫秒ms换算成xx:xx:xx形式
- 简单几步搭建samba服务器
- Here签到网站使用说明书
- hdoj 1240 Asteroids!
- Can't find file: './mysql/plugin.frm' (errno: 13)[mysql数据目录迁移错位]错误解决
- 日常日本語3
- oc第七天:类的扩展
- 控制Linux下 mono 服务的启动停止