Android应用无法读取外部存储文件

Android应用无法读取外部存储文件

本文介绍了Android应用无法读取外部存储文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建仅供私人使用的应用...那么,不要紧,请尊重您的时间:P

I'm creating an app for a private use only... it's not so important then, respect your time :P

我要制作的是一个从数据库读取并播放声音的应用程序.我已经建立了数据库,并且能够从res/raw文件夹播放声音.但是,有数千个文件要播放,一些新文件将成为该应用程序的一部分,而其他文件将在以后删除.因此,添加一个简单的文件使我每次都可以下载整个应用程序(〜1 GB)(或者我错了吗?).相反,我想到了读取Excel文件并从SD卡播放声音的操作.但据我所知...

What I want to make is an app that reads from a database and plays sounds. I've already made the database and I am able to play sound from res/raw folder. However, there are thousands of files to play and some new ones will be a part of the app and other will be deleted in the future. So, adding a simple file makes me to download the whole app (~1 GB) each time (or maybe I'm wrong?). Instead, I thought of reading an Excel file and playing sounds from SD card then. But I can't... as far as i know:

我将文件放入/storage/emulated/0/Android/data/my_package_folder/files 文件夹中.我读了很多话题,没有任何帮助.

I put the files into the /storage/emulated/0/Android/data/my_package_folder/files folder. I've read really many topics and nothing helped me.

Environment.MEDIA_MOUNTED.equals(extStorageState) //returns true
Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState) //returns false

我尝试了几种创建新文件对象或使用媒体播放器播放声音的方法(正如我之前说过的,播放res/raw文件没有问题),但总是 file.exists 返回false或声音不播放:

I tried a few ways of creating new file object or playing the sound with the mediaplayer (as I've said before, there's no problem with playing res/raw files) but file.exists always returns false or sound doesn't play:

String filePath = getExternalFilesDir(null) + "/myfile.mp3"; //returns correct file path
File file = new File(filePath);

OR

File file = new File(getExternalFilesDir(null).getPath(), "myfile.mp3");

OR

MediaPlayer mp;
mp = MediaPlayer.create(this, Uri.parse(getExternalFilesDir(null).getPath() + "/myfile.mp3"));

minSdkVersion设置为19,因此在清单中不需要该行:

The minSdkVersion is set to 19 so I don't need that line in Manifest:

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

我已经包括了这一行,没有任何改变:

I have included that line and nothing changed:

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

我也试图用其他文件扩展名来做到这一点.此外,此处的解决方案也无济于事.我的手机是Samsung Galaxy Grand Prime(SM-G530FZ),Android v.5.0.2.请帮忙! :)

I was also trying to do that with other file extensions. Also, the solution from here didn't help. My phone is Samsung Galaxy Grand Prime (SM-G530FZ), Android v. 5.0.2. Help, please! :)

推荐答案

这是@Nisarg的代码,必须对其进行一些清理:

Here is the code @Nisarg, had to clean it a bit:

import android.Manifest;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;

public class MainActivity extends AppCompatActivity implements OnClickListener {

Button readExcelButton;
static String TAG = "ExelLog";
MediaPlayer mpintro;

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

    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
        Toast.makeText(this, "Permission checking", Toast.LENGTH_SHORT).show();
        checkPermission();
    }

    readExcelButton = (Button) findViewById(R.id.readExcel);
    readExcelButton.setOnClickListener(this);

}

public void onClick(View v)
{
    switch (v.getId())
    {
        case R.id.readExcel:
            String filePath = getExternalFilesDir(null) + "/p0000.mp3";
            File file = new File(filePath);
            file.setReadable(true);
            file.setWritable(true);

            if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
                Toast.makeText(this, "Ext storage not available or read only", Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(this, "Ext storage available", Toast.LENGTH_SHORT).show();
            }

            if (file.exists()){
                Toast.makeText(this, "File found", Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(this, getExternalFilesDir(null) + "/p0000.mp3 - File not found", Toast.LENGTH_LONG).show();
            }

            break;
    }
}

private void checkPermission() {
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this,
            Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {//Can add more as per requirement

        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},
                123);

    } else {

    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 123: {

            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED)     {
                //Peform your task here if any
            } else {

                checkPermission();
            }
            return;
        }
    }
}

public static boolean isExternalStorageReadOnly() {
    String extStorageState = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
        return true;
    }
    return false;
}

public static boolean isExternalStorageAvailable() {
    String extStorageState = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
        return true;
    }
    return false;
}

}

这篇关于Android应用无法读取外部存储文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 01:48