Я пытаюсь реализовать поддержку разрешений Marshmallow в классе MyAdapter для mylistView, но метод onRequestPermissionsResult
не переопределяет и не реализует метод из супертипа ... в то время как я пробовал код разрешения Marshmallow для WRITE_EXTERNAL_STORAGE
дляскачать файл из интернета в другом проекте, он работал без этой ошибки "метод не переопределяет или не реализует метод из супертипа"
Я использую эмулятор Nexus 5X под управлением Android 8.1.0, API 27.
MyAdapter Class
public class MyAdapter extends BaseAdapter {
private static final int PERMISSION_REQUEST_CODE = 1;
String fileName;
String dirPath;
Context context;
ArrayList<StoryModel> TitleAndImage ;
ArrayList<StoryModel> forShowEachStoryInNewActivity ;
LayoutInflater inflter;
public MyAdapter(Context context, ArrayList<StoryModel> TitleAndImage ,ArrayList<StoryModel> forShowEachStoryInNewActivity) {
this.context = context;
this.TitleAndImage = TitleAndImage;
this.forShowEachStoryInNewActivity=forShowEachStoryInNewActivity;
inflter = (LayoutInflater.from(context));
}
@Override
public int getCount() {
return TitleAndImage.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = inflter.inflate(R.layout.item_story, null);
TextView t1 = convertView.findViewById(R.id.title);
ImageView m1= convertView.findViewById(R.id.image);
Button b1 = convertView.findViewById(R.id.buttn_bwonload);
t1.setText(TitleAndImage.get(position).getTitle());
Picasso.get().load(TitleAndImage.get(position).getImage()).resize(200,200).into(m1);
//TODO : going to Show each story in new Activity
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, ForEachStory.class);
String title = forShowEachStoryInNewActivity.get(position).getTitle();
// String image = forShowEachStoryInNewActivity.get(position).getImage();
String body = forShowEachStoryInNewActivity.get(position).getBody();
intent.putExtra("title", title);
// intent.putExtra("image", image);
intent.putExtra("body", body);
context.startActivity(intent);
}
});
//TODO download this story when i click on it's download button
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dirPath = com.example.abdullah.newstroynory.Utils.getRootDirPath(context);
String title_For_Download = forShowEachStoryInNewActivity.get(position).getTitle();
fileName = title_For_Download + ".mp3";
String audio_For_Download = forShowEachStoryInNewActivity.get(position).getAudio();
// String url = audio_For_Download;
//TODO Marshmallow's permission
if (Build.VERSION.SDK_INT >= 23)
{
if (checkPermission())
{
// Code for above or equal 23 API Oriented Device
// Your Permission granted already .Do next code
dorDownloadMyAudios ( fileName, dirPath , audio_For_Download);
} else {
requestPermission(); // Code for permission
}
}
else
{
// Code for Below 23 API Oriented Device
// Do next code
// dorDownloadMyAudios ( fileName, dirPath , audio_For_Download);
}
}
});
return convertView;
}
//TODO code of chick of download from the internet into the SD card
private boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (result == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
private void requestPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Toast.makeText( context, "Write External Storage permission allows us to do store files. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions((Activity) context, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.e("value", "Permission Granted, Now you can use local drive .");
} else {
Log.e("value", "Permission Denied, You cannot use local drive .");
}
break;
}
}
//TODO library of downloading file
private void dorDownloadMyAudios ( String fileName, String dirPath , String url){
PRDownloader.initialize(context);
PRDownloaderConfig config = PRDownloaderConfig.newBuilder()
.setDatabaseEnabled(true)
.build();
PRDownloader.initialize(context, config);
int downloadId = PRDownloader.download(url, dirPath, fileName)
.build()
.setOnStartOrResumeListener(new OnStartOrResumeListener() {
@Override
public void onStartOrResume() {
}
})
.setOnPauseListener(new OnPauseListener() {
@Override
public void onPause() {
}
})
.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel() {
}
})
.setOnProgressListener(new com.downloader.OnProgressListener() {
@Override
public void onProgress(Progress progress) {
}
})
.start(new OnDownloadListener() {
@Override
public void onDownloadComplete() {
// Log.d("--------->", "onCreate: "+dirPath+fileName);
Toast.makeText(context, "Hi", Toast.LENGTH_SHORT).show();
Log.d("------->", "onDownloadComplete: OK ------->");
}
@Override
public void onError(Error error) {
}
});
}
}
это еще один проект, он хорошо работает, скачать аудио файл без ошибок
MainActivity Class
public class MainActivity extends AppCompatActivity {
private static final int PERMISSION_REQUEST_CODE = 1;
String fileName;
String dirPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "https://firebasestorage.googleapis.com/v0/b/storynory-1e523.appspot.com/o/audio%2Fcroesus-solon-storynory.mp3?alt=media&token=3f4b9ff4-8891-4b8d-9ddf-1d461faf80c2";
dirPath = com.example.abdullah.dwonloadaudio.Utils.getRootDirPath(getApplicationContext());
fileName = "myPhoto2.mp3";
if (Build.VERSION.SDK_INT >= 23)
{
if (checkPermission())
{
// Code for above or equal 23 API Oriented Device
// Your Permission granted already .Do next code
dorDownloadMyAudios ( fileName, dirPath , url);
} else {
requestPermission(); // Code for permission
}
}
else
{
// Code for Below 23 API Oriented Device
// Do next code
}
}
private boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (result == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
private void requestPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Toast.makeText(MainActivity.this, "Write External Storage permission allows us to do store files. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.e("value", "Permission Granted, Now you can use local drive .");
} else {
Log.e("value", "Permission Denied, You cannot use local drive .");
}
break;
}
}
private void dorDownloadMyAudios (String fileName, String dirPath , String url){
PRDownloader.initialize(this);
PRDownloaderConfig config = PRDownloaderConfig.newBuilder()
.setDatabaseEnabled(true)
.build();
PRDownloader.initialize(getApplicationContext(), config);
int downloadId = PRDownloader.download(url, dirPath, fileName)
.build()
.setOnStartOrResumeListener(new OnStartOrResumeListener() {
@Override
public void onStartOrResume() {
}
})
.setOnPauseListener(new OnPauseListener() {
@Override
public void onPause() {
}
})
.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel() {
}
})
.setOnProgressListener(new OnProgressListener() {
@Override
public void onProgress(Progress progress) {
}
})
.start(new OnDownloadListener() {
@Override
public void onDownloadComplete() {
Log.d("------->", "onDownloadComplete: OK ");
}
@Override
public void onError(Error error) {
}
});
}
}
примечание: код работает до и после того, как все переопределения стали устаревшими