0

Android アプリケーションで、サーバーからアップロードした写真をデータベースに保存し、後で再利用したいと考えています。それらをバイナリ形式で保存し、それらのリンクをデータベースに保存する必要があると思います。それはより良い解決策ですか?コードまたは例を教えてください。ありがとう。

PS:今は画像をアップロードして ImageView を使用して直接表示するだけですが、ユーザーがオフラインのときにアプリケーションで使用できるようにしたいと考えています。

4

1 に答える 1

0

私の経験では、これを達成するための最良の方法は、インターネットからsdcardに画像を保存することです。これにより、ファイルへのアクセスが高速になります。

私のSDカードに私の画像ディレクトリを作成する機能...

public static File createDirectory(String directoryPath) throws IOException {

    directoryPath = Environment.getExternalStorageDirectory().getAbsolutePath() + directoryPath;
    File dir = new File(directoryPath);
    if (dir.exists()) {
        return dir;
    }
    if (dir.mkdirs()) {
        return dir;
    }
    throw new IOException("Failed to create directory '" + directoryPath + "' for an unknown reason.");
}

例::createDirectory("/jorgesys_images/");

この関数を使用して、インターネットから自分のフォルダの画像をSDカードに保存します

private Bitmap ImageOperations(Context ctx, String url, String saveFilename) {
    try {           
        String filepath=Environment.getExternalStorageDirectory().getAbsolutePath() + "/jorgesys_images/";
        File cacheFile = new File(filepath + saveFilename);
        cacheFile.deleteOnExit();
        cacheFile.createNewFile();
        FileOutputStream fos = new FileOutputStream(cacheFile);
        InputStream is = (InputStream) this.fetch(url);

        BitmapFactory.Options options=new BitmapFactory.Options();
        options.inSampleSize = 8;

        Bitmap bitmap = BitmapFactory.decodeStream(is);
        bitmap.compress(CompressFormat.JPEG,80, fos);
        fos.flush();
        fos.close();
        return bitmap;

    } catch (MalformedURLException e) {         
                    e.printStackTrace();
        return null;
    } catch (IOException e) {
                    e.printStackTrace();        
        return null;
    } 
} 

public Object fetch(String address) throws MalformedURLException,IOException {
    URL url = new URL(address);
    Object content = url.getContent();
    return content;
}

このBitmpapをimageViewに使用し、オフラインの場合はsdcardから直接画像を取得します。

于 2010-08-31T16:39:55.920 に答える