在图库和相机

时间:2017-06-24 14:29:39

标签: android listview android-edittext

我真的不知道如何做到这一点,笔记能够附加相机和图库的照片

image

还有上半部分显示的其他功能,我还没有找到任何关于它的教程。我需要帮助,谢谢。我是初学者

image2

1 个答案:

答案 0 :(得分:0)

这个问题的范围可能有点广泛,但我会尽量总结一下。

为了从用户的第三方图库应用程序渲染图像,您需要先通过最初在清单中设置存储权限来访问其设备存储,如下所示:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

接下来,在运行任何UI线程之前,您必须在运行时(在Android 6.0及更低版本的安装期间)相应地为Android 6.0 / Marshmallow及以上版本授予权限(访问用户的设备存储被视为危险权限) here in the docs。然后打开图库应用程序,比如说,单击一个Button,然后通过onActivityResult()中的Cursor全部使用存储数据,将带有位图的ImageView渲染到所选图像的URI路径。

以下是一个示例活动:

public class MainActivity extends AppCompatActivity {

    // Constant that's used as a parameter to assist with the permission requesting process.
    private static final int PERMISSION_CODE = 100;

    // Int constant that's used to handle the result back when an image is selected from the
    // device's gallery.
    private static final int RESULT_LOAD_IMAGE = 1;

    private ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Requests permission for devices with versions Marshmallow (M)/API 23 or above.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {

                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        PERMISSION_CODE);

                return;
            }
        }

        // The following invoking method either executes for versions older than M, or until the
        // user accepts the in-app permission for the next sessions.
        runUi();
    }

    // Displays a permission dialog when requested for devices M and above.
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults) {
        if (requestCode == PERMISSION_CODE) {

            // User accepts the permission(s).
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // Invoker for rendering UI.
                runUi();
            } else { // User denies the permission.
                Toast.makeText(this, "Please come back and then grant permissions!",
                        Toast.LENGTH_SHORT).show();

                // Runs a thread for a slight delay prior to shutting down the app.
                Thread mthread = new Thread() {
                    @Override
                    public void run() {
                        try {
                            sleep(1500);
                            System.exit(0);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                };

                mthread.start();
            }
        }
    }

    private void runUi() {
        mImageView = (ImageView) findViewById(R.id.image_view);

        // Sets the image button clickable with the following functionality.
        findViewById(R.id.change_img_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // Instantiates an Intent object for accessing the device's storage.
                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                // Triggers the image gallery.
                startActivityForResult(intent, RESULT_LOAD_IMAGE);
            }
        });
    }

    /**
     * Invoked once a third-party app (such as Gallery) is dismissed from its purpose via an
     * implicit intent.
     *
     * @param requestCode is the code constant of the intent's purpose.
     * @param resultCode is the result code constant of the intent.
     * @param data is the actual intent.
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Runs the following code should the code constants and intent match that of selecting an
        // image from the device's gallery.
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null) {

            // References the device's storage URI for images from the intent parameter.
            Uri selectedImageUri = data.getData();

            // Initializes a temporary string list of the image file path as the column to render
            // the image immediately.
            String[] projection = { MediaStore.Images.Media.DATA };

            // References and queries the database with the following parameters, and then moves to
            // the first row index.
            Cursor cursor = getContentResolver().query(
                    selectedImageUri,   // Provider content URI to query
                    projection,         // Columns to include in the resulting Cursor
                    null,               // No selection clause
                    null,               // No selection arguments
                    null);              // Default sort order
            cursor.moveToFirst();

            // Retrieves and assigns the file path as a string value, and then sets the image's
            // bitmap to render it.
            String imgFilePath = cursor.getString(cursor.getColumnIndex(projection[0]));
            mImageView.setImageBitmap(BitmapFactory.decodeFile(imgFilePath));

            // Closes the cursor to release all of its resources.
            cursor.close();
        }
    }
}