sdcard存儲圖片下載簡單操做


效果圖示例:android





步驟 -- 在配置清單添加完聯網權限後緩存

一、res/layout界面佈局ide


代碼工具


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"佈局

    xmlns:tools="http://schemas.android.com/tools"this

    android:layout_width="match_parent"url

    android:layout_height="match_parent"spa

    android:orientation="vertical" >code


    <ImageVieworm

        android:id="@+id/imageview"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />

    

     <Button

        android:id="@+id/bt_download"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="圖片下載"

        android:onClick="Image_Download" />


</LinearLayout>



====================




二、有2個類一個操做SDCARD的文件工具類(FileUtil)另一個MainActivity類


FileUtil類


代碼


//操做SDCARD的文件工具類

public class FileUtil {


private static final String CACHE_DIR = Environment.getExternalStorageDirectory() + "/my_caches/images";


private static int COMP_JPG = 0;

private static int COMP_PNG = 1;

//判斷sdcard 是否掛載(是否有sdcard)

public static boolean isMounted(){

String state = Environment.getExternalStorageState();

return state.equals(Environment.MEDIA_MOUNTED);

}


//獲取sdcard文件 根路徑的絕對路徑

public static String getSDCARD(){

return Environment.getExternalStorageDirectory().getAbsolutePath();

}


//獲取文件名 

public static String getFilename(String url){

return url.substring(url.lastIndexOf('/') + 1);

}


//保存文件 方法1

public static void sava1(String url,byte[] data){

//判斷是否有sdcard

if(!isMounted()){

return ;

}

//有sdcard

//判斷是否有緩存文件夾

File dir = new File(CACHE_DIR);

if(!dir.exists()){

//不存在緩存文件夾 建立文件夾用來保存文件

dir.mkdirs();

}

//把文件 數據存到sdcard

File file = new File(dir,getFilename(url));

try {

FileOutputStream fos = new FileOutputStream(file);

fos.write(data);

fos.close();

} catch (Exception e) {

e.printStackTrace();

}

}

//保存文件 方法2

public static void sava2(String url,Bitmap bitmap,int format){

//判斷 是否有sdcard

if(!isMounted()){

return ;

}

File dir = new File(CACHE_DIR);

if(!dir.exists()){

dir.mkdirs();

}

//把 文件數據 寫到 sdcard

File file = new File(dir,getFilename(url));

try {

FileOutputStream fos = new FileOutputStream(file);

//把圖片文件寫入緩存

bitmap.compress((format == COMP_JPG?CompressFormat.JPEG:CompressFormat.PNG), 100, fos);

fos.close();

} catch (Exception e) {

e.printStackTrace();

}

}

//讀取圖片

public static Bitmap readImage(String url){

if(!isMounted()){

return null;

}

File file = new File(CACHE_DIR,getFilename(url));

if(file.exists()){

return BitmapFactory.decodeFile(file.getAbsolutePath());

}

return null;

}

//清空 緩存目錄

public void clearCaches(){

File dir = new File(CACHE_DIR);

File[] file_datas = dir.listFiles();

for(File file : file_datas){

file.delete();

}

}

}





==============



MainActivity類


代碼


public class MainActivity extends Activity {


private ImageView imageview;

private String url = "http://b.hiphotos.baidu.com/image/pic/item/d1160924ab18972bf5f68cc8e0cd7b899f510ae7.jpg";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

this.imageview = (ImageView) this.findViewById(R.id.imageview);

//設置默認圖片

imageview.setImageResource(R.drawable.ic_launcher);

}

//圖片下載 按鈕 點擊事件 監聽

public void Image_Download(View view){

//判斷本地是否有圖片

Bitmap bitmap = FileUtil.readImage(url);

if(bitmap != null){

imageview.setImageBitmap(bitmap);

}else{

//沒有圖片 就發送聯網請求

new MyAsyncTask().execute(url);

}

}

//聯網工具類

public class MyAsyncTask extends AsyncTask<String, Void, byte[]>{


@Override

protected byte[] doInBackground(String... params) {

HttpGet get = new HttpGet(params[0]);

HttpClient client = new DefaultHttpClient();

HttpResponse response;

try {

response = client.execute(get);

if(response.getStatusLine().getStatusCode() == 200){

byte[] data = EntityUtils.toByteArray(response.getEntity());

FileUtil.sava1(params[0], data);

return data;

}

}

catch (Exception e) {

e.printStackTrace();

}

return null;

}

@Override

protected void onPostExecute(byte[] result) {

super.onPostExecute(result);

if(result != null){

Bitmap bitmap = BitmapFactory.decodeByteArray(result, 0, result.length);

imageview.setImageBitmap(bitmap);

}

}

}

}

相關文章
相關標籤/搜索