android-如何将视图保存到SD卡

时间:2015-03-20 12:28:25

标签: android bitmap

我有一个imageview,我试图通过这种方法从imageview中保存位图

bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.close();

保存的图像的rgb不像它在运行的应用程序中看到的那样,所以我想知道是否有任何方法可以将图像视图直接保存到SD卡而不是获取位图然后将其保存到SD卡。 请帮助我,我已经尝试了一切。

2 个答案:

答案 0 :(得分:1)

您可以使用以下代码读取和写入对象:

public static void witeObjectToFile(Context context, Object object, String filename)
{
    ObjectOutputStream objectOut = null;
    try
    {
        FileOutputStream fileOut = context.openFileOutput(filename, Activity.MODE_PRIVATE);
        objectOut = new ObjectOutputStream(fileOut);
        objectOut.writeObject(object);
        fileOut.getFD().sync();

    } catch (IOException e)
    {
        e.printStackTrace();
    } finally
    {
        if (objectOut != null)
        {
            try
            {
                objectOut.close();
            } catch (IOException e)
            {
                // do nowt
            }
        }
    }
}

public static Object readObjectFromFile(Context context, String filename)
{
    ObjectInputStream objectIn = null;
    Object object = null;
    try
    {
        FileInputStream fileIn = context.getApplicationContext().openFileInput(filename);
        objectIn = new ObjectInputStream(fileIn);
        object = objectIn.readObject();

    } catch (FileNotFoundException e)
    {
        // Do nothing
    } catch (IOException e)
    {
        e.printStackTrace();
    } catch (ClassNotFoundException e)
    {
        e.printStackTrace();
    } finally
    {
        if (objectIn != null)
        {
            try
            {
                objectIn.close();
            } catch (IOException e)
            {
                // do nowt
            }
        }
    }

    return object;
}

例如,ArrayList可以保存为:

ImageView abcImage = (ImageView) readObjectFromFile(context, AppConstants.FILE_PATH_TO_DATA);

并写为:

witeObjectToFile(context, abcImage, AppConstants.FILE_PATH_TO_DATA);

答案 1 :(得分:1)

尝试使用此

   public void onClick(View v) {

    if (v.getId() == R.id.btnSaveImage) {
        imageView.setDrawingCacheEnabled(true);
        Bitmap bm = imageView.getDrawingCache();
        storeImage(bm);
    }
}

private boolean storeImage(Bitmap imageData) {

    // get path to external storage (SD card)
    String iconsStoragePath = Environment.getExternalStorageDirectory() + "/yourappname/";
    File sdIconStorageDir = new File(iconsStoragePath);

    // create storage directories, if they don't exist
    sdIconStorageDir.mkdirs();

    try {
        File file = new File(sdIconStorageDir.toString() + File.separator + "fileName");

        FileOutputStream fileOutputStream = new FileOutputStream(file);
        BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);

        imageData.compress(CompressFormat.PNG, 100, bos);

        bos.flush();
        bos.close();

        MediaScannerConnection.scanFile(this, new String[] { file.getPath() },
                new String[] { "image/jpeg" }, null);

        Toast.makeText(this, "Snapshot Saved to " + file, Toast.LENGTH_SHORT).show();

    } catch (FileNotFoundException e) {
        return false;
    } catch (IOException e) {
        return false;
    }
    return true;
}