【问题标题】:Why I have Permission denied on a file located in /data/user_de/为什么我对位于 /data/user_de/ 的文件的权限被拒绝
【发布时间】:2019-12-07 16:14:27
【问题描述】:

我想获取 MMS 的附加内容,例如图像或视频/音频。 首先我做这个

static void getMmsContent(Context context, ArrayList<Mms> mmsArrayList) {
        try {
            for (Mms unMms : mmsArrayList) {

                ContentResolver contentResolver = context.getContentResolver();
                Uri uri = Uri.parse("content://mms/part");
                String selection = Telephony.Mms.Part.MSG_ID + "=" + unMms.getId();

                Cursor query = contentResolver.query(uri, null, selection, null, null);

                if (query != null && query.moveToFirst()) {
                    do {
                        String name = query.getString(query.getColumnIndex("name"));
                        String type = query.getString(query.getColumnIndex("ct"));
                        String txt = query.getString(query.getColumnIndex(Telephony.Mms.Part.TEXT));
                        String data = query.getString(query.getColumnIndex(Telephony.Mms.Part._DATA));

                        if (!type.equals("application/smil")) {
                            String[] dataMms = {name, type, txt, data};
                            getContent(context, dataMms, unMms);
                        }
                    } while (query.moveToNext());
                }
                if (query != null) {
                    query.close();
                }
            }
        } catch (Exception e) {
            Log.d("Exception", e.toString());
        }
    }

这一行给了我附加内容所在位置的路径。

字符串数据 = query.getString(query.getColumnIndex(Telephony.Mms.Part._DATA));

/data/user_de/0/com.android.providers.telephony/app_parts/PART_1555841710097_Screenshot_20190421-121445_Chrome1.jpg

所以现在我想将图像转换为位图以将其添加到 zip 文件中。

static private void getContent(Context context, String[] dataMms, Mms unMms){
        if (dataMms[1].equals("text/plain")) {
            unMms.setCorps(dataMms[2]);
        } else {
            if ("image/jpeg".equals(dataMms[1]) || "image/bmp".equals(dataMms[1]) ||
                    "image/gif".equals(dataMms[1]) || "image/jpg".equals(dataMms[1]) ||
                    "image/png".equals(dataMms[1])) {
                unMms.setTypeContenu(dataMms[1]);

                Bitmap bitmap = null;
                InputStream is = null;
                try {
                    File source = new File(dataMms[3]);
                    is = new FileInputStream(source);
                    bitmap = BitmapFactory.decodeStream(is);
                } catch (IOException e) {
                    Log.d("Exception", e.toString());
                } finally {
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException e) {
                            Log.d("Exception", e.toString());
                        }
                    }
                }
                if (bitmap != null) {
                    File file = new File(context.getApplicationInfo().dataDir + "/files/", dataMms[0]);
                    OutputStream Fout = null;
                    try {
                        Fout = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, Fout);
                        Fout.flush();
                        Fout.close();
                    } catch (FileNotFoundException e) {
                        Log.d("Exception", e.toString());
                    } catch (IOException e) {
                        Log.d("Exception", e.toString());
                    }

                }
            }
        }
    }

但是我的代码在 new FileInputStream(source) 上抛出异常;

我收到了

D/Exception: java.io.FileNotFoundException: /data/user_de/0/com.android.providers.telephony/app_parts/PART_1547316880687_Resized_20190112_191438_9422.jpeg (Permission denied)

我有权限,我需要用户权限。

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

【问题讨论】:

  • 您正试图侵入另一个应用程序的内部存储。这是行不通的。我不知道你是怎么走上这条路的。我怀疑你可以从SmsContentProvider 获得Uri,然后使用ContentResolveropenInputStream() 来访问该内容。

标签: java android android-permissions permission-denied mms


【解决方案1】:

所以我在 CommonsWare 的评论之后将我的代码更改为:

static private void getContent(Context context, String[] dataMms, Mms unMms) {
        if (dataMms[1].equals("text/plain")) {
            unMms.setCorps(dataMms[2]);
        } else {
            if ("image/jpeg".equals(dataMms[1]) || "image/bmp".equals(dataMms[1]) ||
                    "image/gif".equals(dataMms[1]) || "image/jpg".equals(dataMms[1]) ||
                    "image/png".equals(dataMms[1])) {
                unMms.setTypeContenu(dataMms[1]);
                Uri partURI = Uri.parse("content://mms/part/" + dataMms[4]);
                InputStream is = null;
                Bitmap bitmap = null;
                try {
                    is = context.getContentResolver().openInputStream(partURI);
                    bitmap = BitmapFactory.decodeStream(is);
                } catch (IOException e) {
                    Log.d("Exception", e.toString());
                } finally {
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException e) {
                            Log.d("Exception", e.toString());
                        }
                    }
                }
                if (bitmap != null) {
                    File file = new File(context.getApplicationInfo().dataDir + "/files/", dataMms[0]);
                    OutputStream Fout = null;
                    try {
                        file.createNewFile();
                        Fout = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, Fout);
                        Fout.flush();
                        Fout.close();
                    } catch (FileNotFoundException e) {
                        Log.d("Exception", e.toString());
                    } catch (IOException e) {
                        Log.d("Exception", e.toString());
                    }
                }
            }
        }
    }

棘手的部分是:

Uri partURI = Uri.parse("content://mms/part/" + dataMms[4]);

我的 dataMms[4] 是 MMS 部分的 id,我从我放在 getMmsContent() 上的这一行得到它:

String id = query.getString(query.getColumnIndex("_id"));

这个专栏给我零件的ID。

但是在 Android 开发者文档中没有提到这个专栏:https://developer.android.com/reference/android/provider/Telephony.Mms.Part.html

所以我在 getMmsContent() 中列出了带有此代码的列,然后我找到了它:

for (int i = 0; i < query.getColumnCount(); i++) {
    Log.i("Column", query.getColumnName(i)); 
    }

现在它正在工作!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 2020-09-12
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    相关资源
    最近更新 更多