I created a app able to download the non media file. Now I plan to extend the app to move the downloaded file (filetype=map)
.
I'm using compileSdkVersion 29
and targetSdkVersion 29
.
This https://developer.android.com/training/data-storage say's I have to use "Storage Access Framework".
I used the snipped from here "How to list all pdf files on android 10?"
but it does only work for media files (uri to file).
Does anybody know where to find a working snipped?
Asked
Active
Viewed 2,249 times
0

Vivek Singh
- 1,142
- 14
- 32

hatietz
- 1
- 1
-
Nobody will follow your links. So nobody will help. Edit your post in such a way that we know what you want and what you are doing. – blackapps Jun 09 '20 at 09:32
1 Answers
0
What has to be changed in the following code in order to copy a file like "whatever.map"? The important part of the question is the file type "map" which means it is a non media file and the mime type is unknown. The target sdk has to be 29. When I - with the following code - pick a file like "whatever.png" it works. But renaming the file "whatever.map" to "whatever.png" before copy in /storage/emulated/0/Download is also not possible with target sdk 29.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/open_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="104dp"
android:layout_marginTop="144dp"
android:onClick="openFile"
android:text="Select"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/save_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="188dp"
android:layout_marginTop="280dp"
android:onClick="saveFile"
android:text="Copy"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/open_button"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
package org.hagerti.storagedemo;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.icu.text.SimpleDateFormat;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
final static String className = MainActivity.class.getSimpleName();
private static final int CREATE_REQUEST_CODE = 40;
private static final int OPEN_REQUEST_CODE = 41;
private static final int SAVE_REQUEST_CODE = 42;
final private static String myMimeType = "image/png";
//final private static String myMimeType = "text/map";
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri currentUri = null;
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SAVE_REQUEST_CODE) {
if (data != null) {
currentUri = data.getData();
writeFileContent(currentUri);
}
} else if (requestCode == OPEN_REQUEST_CODE) {
if (data != null) {
currentUri = data.getData();
try {
String content = readFileContent(currentUri);
} catch (IOException e) {
}
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void openFile(View view) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType(myMimeType);
startActivityForResult(intent, OPEN_REQUEST_CODE);
/**
* http://www.iana.org/assignments/media-types/media-types.xhtml
* Often used media types:
* image/jpeg
* audio/mpeg4-generic
* text/html
* audio/mpeg
* audio/aac
* audio/wav
* audio/ogg
* audio/midi
* audio/x-ms-wma
* video/mp4
* video/x-msvideo
* video/x-ms-wmv
* image/png
* image/jpeg
* image/gif
* .xml ->text/xml
* .txt -> text/plain
* .cfg -> text/plain
* .csv -> text/plain
* .conf -> text/plain
* .rc -> text/plain
* .htm -> text/html
* .html -> text/html
* .pdf -> application/pdf
* .apk -> application/vnd.android.package-archive
*/
}
private String readFileContent(Uri uri) throws IOException {
Log.e(className, " readFileContent uri.getPath()=" + uri.getPath());
InputStream inputStream =
getContentResolver().openInputStream(uri);
BufferedReader reader =
new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String currentline;
while ((currentline = reader.readLine()) != null) {
stringBuilder.append(currentline + "\n");
}
inputStream.close();
return stringBuilder.toString();
}
public void saveFile(View view) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
//intent.setType("text/plain");
intent.setType(myMimeType);
startActivityForResult(intent, SAVE_REQUEST_CODE);
}
private void writeFileContent(Uri uri) {
Log.e(className, " writeFileContent uri.getPath()=" + uri.getPath());
FileChannel inChannel = null;
FileChannel outChannel = null;
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File expFile = new File("/storage/emulated/0/Android/data/org.hagerti.storagedemo/files" + File.separator + timeStamp + ".map");
try {
inChannel = new FileInputStream(getContentResolver().openFileDescriptor(uri, "r").getFileDescriptor()).getChannel();
outChannel = new FileOutputStream(expFile).getChannel();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inChannel != null)
try {
inChannel.close();
Log.e(className, " writeFileContent inChannel closed");
} catch (IOException e) {
e.printStackTrace();
}
if (outChannel != null)
try {
outChannel.close();
Log.e(className, " writeFileContent outChannel closed");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

hatietz
- 1
- 1
-
-
It seems to be there is no solution for sdk 29. Normally I'm possitiv related to android design. But being able to load down any file in the download folder and NOT being able to process (read, copy or move a file (mimetype unknown ) is not acceptable. The entire security design is a pain in the ass and spaghetti code, – hatietz Jun 25 '20 at 13:00