Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取图像(URI)的路径并将其发送到数据库?_Java_Android_Xml - Fatal编程技术网

Java 如何获取图像(URI)的路径并将其发送到数据库?

Java 如何获取图像(URI)的路径并将其发送到数据库?,java,android,xml,Java,Android,Xml,如果我点击按钮,我有一个代码来获取图像,在我的问题中,我想获取图像的URI/路径并将其发送到数据库,然后我想在ImageView中显示它。这是我的密码 @Override public void onActivityResult(int req, int res, Intent imageIntent) { super.onActivityResult(req, res, imageIntent); switch (req) { case SELE

如果我点击按钮,我有一个代码来获取图像,在我的问题中,我想获取图像的URI/路径并将其发送到数据库,然后我想在ImageView中显示它。这是我的密码

       @Override
public void onActivityResult(int req, int res, Intent imageIntent) {
    super.onActivityResult(req, res, imageIntent);

    switch (req) {
        case SELECT_ICON:
            if (res == RESULT_OK) {
                try {
                    final Uri iconUri = imageIntent.getData();
                    final String imgUri = iconUri.toString();
                    final InputStream iconStream = getContentResolver().openInputStream(iconUri);
                    final Bitmap selectedIcon = BitmapFactory.decodeStream(iconStream);
                    imageView.setImageBitmap(selectedIcon);

                    saveButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View arg0) {
                            String tname = text1.getText().toString();
                            String date = textView.getText().toString();
                            String priority = spinner1.getSelectedItem().toString();
                            String notes = text2.getText().toString();
                            String path = imgUri;

                            dbHelper.addTask(new Task(tname, date, priority, notes, path));
                            Toast.makeText(getApplicationContext(), "Success!!!", Toast.LENGTH_LONG).show();
                            TaskListActivity.tasklist.RefreshList();
                            finish();
                        }
                    });
                } catch (FileNotFoundException err) {
                    err.printStackTrace();
                }
            }
    }
}

我使用这些方法从URI获取路径

private String getActualFilePath(Context context, Uri fileUri) throws URISyntaxException{
    Uri uri = Uri.parse(fileUri);
    final boolean needToCheckUri = Build.VERSION.SDK_INT >= 19;
    String selection = null;
    String[] selectionArgs = null;
    // Uri is different in versions after KITKAT (Android 4.4), we need to
    // deal with different Uris.
    if (needToCheckUri && DocumentsContract.isDocumentUri(context.getApplicationContext(), uri)) {
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            return Environment.getExternalStorageDirectory() + "/" + split[1];
        } else if (isDownloadsDocument(uri)) {
            final String id = DocumentsContract.getDocumentId(uri);
            uri = ContentUris.withAppendedId(
                    Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
        } else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];
            if ("image".equals(type)) {
                uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }
            selection = "_id=?";
            selectionArgs = new String[]{ split[1] };
        }
    }
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } catch (Exception e) {
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }
    return null;
}
 /**
 * @param uri The Uri to check.
 * @return Whether the Uri authority is ExternalStorageProvider.
 */
public static boolean isExternalStorageDocument(Uri uri) {
    return "com.android.externalstorage.documents".equals(uri.getAuthority());
}

/**
 * @param uri The Uri to check.
 * @return Whether the Uri authority is DownloadsProvider.
 */
public static boolean isDownloadsDocument(Uri uri) {
    return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}

/**
 * @param uri The Uri to check.
 * @return Whether the Uri authority is MediaProvider.
 */
public static boolean isMediaDocument(Uri uri) {
    return "com.android.providers.media.documents".equals(uri.getAuthority());
}
 private void LogException(Exception ex){
    if(ex != null){
        try{
            ex.printStackTrace(logPrintWriter);
            String data = logStringWriter.toString();

            AddErrorLogEntry(data);
        }finally {
            Log.d(TAG, "Logged exception in file");
        }
    }
 }

您可以使用String-strUri=uri.toString将uri转换为字符串将此strUri保存到数据库,在从数据库检索时,您可以使用uri-uri=uri.parsestrUri将字符串转换回uri

查看库以加载图像您的问题是什么?你是否有任何异常或其他情况我想从存储器中选取一个图像,并将该图像的路径保存到数据库中,我想显示该图像及其到ImageView的路径你有最终的Uri iconUri,你还需要什么?我想将iconUri放入数据库似乎只是覆盖了现有的数据库,我通过重新安装应用程序来修复它。我想检索uri,有什么问题吗?字符串解析=cursor.getString4;Uri=Uri.parseparse;imageView.setImageURIuri;不很好,如果你现在有错误的话。请ping,但图像仍然没有显示