【问题标题】:Converting a String to Bitmap, and Store it to SD Card将字符串转换为位图,并将其存储到 SD 卡
【发布时间】:2013-06-26 02:49:03
【问题描述】:

我想通过首先转换返回的包含 base64 的 JSONObject,然后将其转换为位图来将图像存储到 SD 卡。我尝试了下面的代码,但似乎没有任何效果。

返回 JSON 对象的 PHP 代码:

            $img = $row["imageName"];
        $b64img = mysql_real_escape_string($b64img);
        $b64img = base64_encode ($img);
        $product["imageName"] = $b64img;

检索返回的 JSON 的 Java 代码:

for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);


byte[] decodedString = Base64.decode(c.getString(TAG_IMAGE_NAME), Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

String fileN = (i+1)+".jpg";

OutputStream fOut = null;
File file = new File(Environment.getExternalStorageDirectory() + "/FinalTestforDatabase/",fileN);
                try {
                fOut = new FileOutputStream(file);
                } catch (FileNotFoundException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                            }
                            decodedByte.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                            try {
                                fOut.flush();
                            } catch (IOException e2) {
                                // TODO Auto-generated catch block
                                e2.printStackTrace();
                            }
                            try {
                                fOut.close();
                            } catch (IOException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                            }

                try {
                    MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
}

【问题讨论】:

  • 在设备端你成功获取JSON数据了吗?你有没有尝试将位图打印到imageview来测试它?
  • 是的,我打印了 decodedString 和 c.getString(TAG_IMAGE_NAME),但是在将位图显示到 imgView.setImageBitmap(decodedByte) 时出现错误。上面的代码有什么问题吗?
  • 你遇到了哪个错误??
  • 是的,请告诉我们您遇到了什么错误?请在此处打印堆栈跟踪 :)

标签: java php android json


【解决方案1】:

我必须确保首先创建主文件夹以包含图像,然后下面的代码才起作用。接下来是创建一个包含图像的新文件夹,所以我需要"/FolderName/FolderName2/"

                    OutputStream fOut = null;
                        File file = new File(
                                Environment.getExternalStorageDirectory()
                                        + "/FolderName/FolderName2/",
                                fileN);
                        try {
                            fOut = new FileOutputStream(file);
                        } catch (FileNotFoundException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                        decodedByte.compress(
                                Bitmap.CompressFormat.JPEG, 85, fOut);
                        try {
                            fOut.flush();
                        } catch (IOException e2) {
                            // TODO Auto-generated catch block
                            e2.printStackTrace();
                        }
                        try {
                            fOut.close();
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }

                        try {
                            MediaStore.Images.Media.insertImage(
                                    getContentResolver(),
                                file.getAbsolutePath(),
                                    file.getName(), file.getName());
                        } catch (FileNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    • 2022-07-26
    • 2012-08-04
    • 2017-07-10
    相关资源
    最近更新 更多