将字符串转换为位图,并将其存储到SD卡

时间:2013-06-26 02:49:04

标签: java php android json

我想通过首先转换包含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();
                }
}

1 个答案:

答案 0 :(得分:0)

我必须确保首先创建主文件夹以包含图像,然后在下面的代码执行操作。接下来要创建一个包含图像的新文件夹,这样我就需要"/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();
                        }