【问题标题】:android image save to res/drawable folder [duplicate]android图像保存到res / drawable文件夹[重复]
【发布时间】:2011-07-25 02:32:53
【问题描述】:

我想将图像保存到我的本地驱动器文件夹或应用程序中的res/drawable 文件夹。我现在正在将 img 保存到 sd 卡中,但我必须将其保存在 res/drawable 文件夹中。 我的代码是:

String image_URL = "http://chart.apis.google.com/chart?chs=200x200&cht=qr&chl=http%3A%2F%2Fandroid-er.blogspot.com%2F";

String extStorageDirectory;

Bitmap bm;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button buttonSave = (Button) findViewById(R.id.save);

    ImageView bmImage = (ImageView) findViewById(R.id.image);
    BitmapFactory.Options bmOptions;
    bmOptions = new BitmapFactory.Options();
    bmOptions.inSampleSize = 1;
    bm = LoadImage(image_URL, bmOptions);
    bmImage.setImageBitmap(bm);


    extStorageDirectory = Environment.getExternalStorageState().toString();
    extStorageDirectory = Environment.getExternalStorageDirectory()
            .toString();

    buttonSave.setText("Save to " + extStorageDirectory + "/qr.PNG");
    buttonSave.setOnClickListener(buttonSaveOnClickListener);
}

private Bitmap LoadImage(String URL, BitmapFactory.Options options) {
    Bitmap bitmap = null;
    InputStream in = null;
    try {
        in = OpenHttpConnection(URL);
        bitmap = BitmapFactory.decodeStream(in, null, options);
        in.close();
    } catch (IOException e1) {
    }
    return bitmap;
}

private InputStream OpenHttpConnection(String strURL) throws IOException {
    InputStream inputStream = null;
    URL url = new URL(strURL);
    URLConnection conn = url.openConnection();

    try {
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            inputStream = httpConn.getInputStream();
        }
    } catch (Exception ex) {
    }
    return inputStream;
}

Button.OnClickListener buttonSaveOnClickListener = new Button.OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        OutputStream outStream = null;
        File file = new File(extStorageDirectory, "er.PNG");
        try {
            outStream = new FileOutputStream(file);
            bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
            outStream.flush();
            outStream.close();

            Toast.makeText(LoadSaveImgActivity.this, "Saved",
                    Toast.LENGTH_LONG).show();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(LoadSaveImgActivity.this, e.toString(),
                    Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(LoadSaveImgActivity.this, e.toString(),
                    Toast.LENGTH_LONG).show();
        }

    }

};

【问题讨论】:

    标签: android image save


    【解决方案1】:

    无法在运行时将任何内容保存到 res/drawable,抱歉。

    【讨论】:

      【解决方案2】:

      这是不可能的。

      以下链接可能会有所帮助

      【讨论】:

        【解决方案3】:

        在运行时,您既不能修改 res/drawable,也不能修改 apk 的任何其他文件夹。它是在编译时构建的,之后无法从应用程序内部对其进行修改。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-10-11
          • 1970-01-01
          • 1970-01-01
          • 2015-11-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多