【问题标题】:Attach image on edittext from gallery and camera将图像附加到画廊和相机的编辑文本上
【发布时间】:2017-06-24 14:29:38
【问题描述】:

我真的不知道该怎么做,笔记可以附加相机和图库中的照片

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

【问题讨论】:

  • 有人帮忙 :(((
  • 好吧,也许您可​​以先接受队列中的编辑,因为您的帖子格式显然需要修改。

标签: android listview android-edittext


【解决方案1】:

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

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

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

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

这是一个示例 Activity:

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();
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多