Android将图像保存到SQLite或SDCard或内存

时间:2012-02-14 07:08:38

标签: android sqlite

我需要从服务器获取图像和其他一些数据,然后在List中显示它。但由于记录数量可能非常大,所以我不确定是否应将图像保存到SQLite数据库或将其保存到SDCard或将其保存到内存中。

谢谢, 没有

5 个答案:

答案 0 :(得分:18)

始终养成保存图像数据库路径的习惯。对于列表视图,请确保只使用这些图像的缩略图。这将有助于您快速加载列表中的这些图像。

 long selectedImageUri = ContentUris.parseId(Uri.parse(anniEntry.getUri()));
 Bitmap bm = MediaStore.Images.Thumbnails.getThumbnail(
                    mContext.getContentResolver(), selectedImageUri,MediaStore.Images.Thumbnails.MICRO_KIND,
                    null );

这里anniEntry.getUri()是图片uri.Now,把它放在第二个代码中。我可以根据需要获得微缩图或迷你缩略图

答案 1 :(得分:12)

通常认为将二进制数据保存为数据库中的图像是不好的形式。在大多数情况下,由于某些技术原因,它实际上最终会损害数据库的性能。而是将要缓存的图像保存到SD卡中,并将这些图像的文件路径存储在数据库中。

答案 2 :(得分:3)

如果你必须下载图像&保存在SDcard.Kindly遵循以下代码:

package com.tablet;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.UnknownHostException;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

/*
 * This class download & store the media in external storage 
 * */
public class DownloadActivity extends Activity {

    private static String fileName = "android.jgp";

    private Button btnDownload;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnDownload = (Button) findViewById(R.id.download);
        btnDownload.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // TODO Auto-generated method stub
                download();
            }
        });


    private void download()
    {
        System.out.println("Inside Download");
        try {
            // this is the file you want to download from the remote server
            String path = "http://code.google.com/android/goodies/wallpaper/android-wallpaper5_1024x768.jpg";

            URL u = new URL(path);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            String PATH = "/data/Kamal"
                //Environment.getExternalStorageDirectory()
            // + "/download/";
                    + "/";

            Log.v("log_tag", "PATH: " + PATH);
            File file = new File(PATH);
            file.mkdirs();
            File outputFile = new File(file, fileName);

            FileOutputStream f = new FileOutputStream(outputFile);
            InputStream in = c.getInputStream();
            Log.v("log_tag"," InputStream consist of : "+in.read());
            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = in.read(buffer)) > 0) {
                //System.out.println("Reading byte : "+in.read(buffer));
                f.write(buffer, 0, len1);
            }
            Toast.makeText(this, "Download Completed Successfully @ "+PATH, Toast.LENGTH_LONG).show();
            f.close();


        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(this, "MalformedURLException", Toast.LENGTH_LONG).show();
        } catch (ProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(this, "ProtocolException", Toast.LENGTH_LONG).show();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(this, "FileNotFoundException", Toast.LENGTH_LONG).show();
        }catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(this, "UnknownHostException", Toast.LENGTH_LONG).show();
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(this, "IOException", Toast.LENGTH_LONG).show();
        }
    }



}

答案 3 :(得分:1)

这是应用要求及其实施的问题。我建议使用内部或SD卡来存储图像并在db

中保存路径

答案 4 :(得分:1)

根据所应用的查询(如图像,视频)存储不需要返回该数据子部分行为的任何重型数据,不应存储在数据库中,而应仅存储对该数据的引用