如何在应用程序的自定义文件夹中保存点击的图像? [英] How to save clicked image in a custom folder of the app?

查看:54
本文介绍了如何在应用程序的自定义文件夹中保存点击的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I want the image clicked by the camera to be saved in my custom folder "MyCameraApp" and that image to be displayed in the app's listview.

Below is the TakePhoto() code:
    
    /**
     * take a photo
     */
    private void activeTakePhoto() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            String fileName = "temp.jpg";
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.TITLE, fileName);
            mCapturedImageURI = getContentResolver()
                    .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                            values);
            takePictureIntent
                    .putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }


The TakePhoto() function calls REQUEST_IMAGE_CAPTURE:

    case REQUEST_IMAGE_CAPTURE:
                if (requestCode == REQUEST_IMAGE_CAPTURE &&
                        resultCode == RESULT_OK) {
                    String[] projection = {MediaStore.Images.Media.DATA};
                    Cursor cursor =
                            managedQuery(mCapturedImageURI, projection, null,
                                    null, null);
                    int column_index_data = cursor.getColumnIndexOrThrow(
                            MediaStore.Images.Media.DATA);
                    cursor.moveToFirst();
                    String picturePath = cursor.getString(column_index_data);
                    MyImage image = new MyImage();
                    image.setTitle("Test");
                    image.setDescription(
                            "test take a photo and add it to list view");
                    image.setDatetime(System.currentTimeMillis());
                    image.setPath(picturePath);
                    images.add(image);
                    daOdb.addImage(image);
                    imageAdapter.notifyDataSetChanged();
                    listView.invalidateViews();
                }
        }

推荐答案

你的问题肯定没有反映你尝试了什么或者您遇到了什么例外。您需要将问题分成不同的解决方案。



您可以通过以下代码将捕获的图像保存到特定文件夹:

Your question certainly doesn't reflect on what have you tried or what exception did you faced.All you need to separate your problem into different solutions.

You can save captured image to a specific folder by this code:
private void createDirectoryAndSaveImage(Bitmap imageToSave, String fileName) {

    File direct = new File(Environment.getExternalStorageDirectory() + "/DirName");

    if (!direct.exists()) {
        File wallpaperDirectory = new File("/sdcard/DirName/");
        wallpaperDirectory.mkdirs();
    }

    File file = new File(new File("/sdcard/DirName/"), fileName);
    if (file.exists()) {
        file.delete();
    }
    try {
        FileOutputStream out = new FileOutputStream(file);
        imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}



要将它们显示到列表视图中,您可以关注它:

从相机中捕捉图像并在Android中设置为listview [ ^ ]


这篇关于如何在应用程序的自定义文件夹中保存点击的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆