Android プロジェクトにファイル ピッカーを実装しようとしています。私がこれまでにできたことは次のとおりです。
Intent chooseFile;
Intent intent;
chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setType("*/*");
intent = Intent.createChooser(chooseFile, "Choose a file");
startActivityForResult(intent, PICKFILE_RESULT_CODE);
そして、私の中でonActivityResult()
switch(requestCode){
case PICKFILE_RESULT_CODE:
if(resultCode==-1){
Uri uri = data.getData();
String filePath = uri.getPath();
Toast.makeText(getActivity(), filePath,
Toast.LENGTH_LONG).show();
}
break;
}
これはファイルピッカーを開いていますが、それは私が望むものではありません。たとえば、ファイル (.txt) を選択し、それを取得してFile
使用したいとします。このコードを使用すると、完全なパスを取得できると思いましたが、そうではありません。たとえば、次のようになります/document/5318/
。しかし、このパスではファイルを取得できません。PathToFile()
を返すというメソッドを作成しましたFile
:
private File PathToFile(String path) {
File tempFileToUpload;
tempFileToUpload = new File(path);
return tempFileToUpload;
}
私がやろうとしているのは、ユーザーがどこからでも a を選択できるようにするFile
ことですDropBox
, Drive
, SDCard
,Mega
など...しかし、それは機能しないので、それ自体を取得する方が良いと思います。次に、これをプログラムで使用して、これまたは.Path
File
Path
File
File
Copy
Delete
EDIT (現在のコード)
じぶんのIntent
Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.addCategory(Intent.CATEGORY_OPENABLE);
chooseFile.setType("text/plain");
startActivityForResult(
Intent.createChooser(chooseFile, "Choose a file"),
PICKFILE_RESULT_CODE
);
そこで何がサポートされているのかわからないので質問なのtext/plain
ですが、調べてみますが、今のところ関係ありません。
私は@Lukas Knuth answeronActivityResult()
と同じものを使用しましたが、それで別の部分にこれができるかどうかはわかりません。彼の答えを待っています。Copy
File
SDcard
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICKFILE_RESULT_CODE && resultCode == Activity.RESULT_OK){
Uri content_describer = data.getData();
//get the path
Log.d("Path???", content_describer.getPath());
BufferedReader reader = null;
try {
// open the user-picked file for reading:
InputStream in = getActivity().getContentResolver().openInputStream(content_describer);
// now read the content:
reader = new BufferedReader(new InputStreamReader(in));
String line;
StringBuilder builder = new StringBuilder();
while ((line = reader.readLine()) != null){
builder.append(line);
}
// Do something with the content in
text.setText(builder.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
getPath()
@YS さんから
私はこれをやっています:
String[] projection = { MediaStore.Files.FileColumns.DATA };
Cursor cursor = getActivity().getContentResolver().query(content_describer, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(projection[0]);
cursor.moveToFirst();
cursor.close();
Log.d( "PATH-->",cursor.getString(column_index));
取得していNullPointerException
ます:
java.lang.RuntimeException: 結果 ResultInfo{who=null, request=131073, result=-1, data=Intent { dat=file:///path typ=text/plain flg=0x3 }} をアクティビティ {info に配信できませんでした.androidhive.tabsswipe/info.androidhive.tabsswipe.MainActivity2}: java.lang.NullPointerException
@YS、@Lukas Knuth、および@CommonsWareのおかげでコードが機能するように編集します。
これは、Intent
ファイルのみを受け入れる場所ですtext/plain
。
Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.addCategory(Intent.CATEGORY_OPENABLE);
chooseFile.setType("text/plain");
startActivityForResult(
Intent.createChooser(chooseFile, "Choose a file"),
PICKFILE_RESULT_CODE
);
のデータを取得onActivityResult()
する場所を作成し、実行中の絶対パスを保存する場所を作成し、それをwith で使用するためにパスの名前を保持します(@YS が知らなかったのは素晴らしいことでした)その関数)、私が呼び出した秒を作成し、これを作成できるに送信します。URI
Intent
File
content_describer.getPath();
TextView
content_describer.getLastPathSegment();
File
destination
AbsolutePath
File
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICKFILE_RESULT_CODE && resultCode == Activity.RESULT_OK){
Uri content_describer = data.getData();
String src = content_describer.getPath();
source = new File(src);
Log.d("src is ", source.toString());
String filename = content_describer.getLastPathSegment();
text.setText(filename);
Log.d("FileName is ",filename);
destination = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Test/TestTest/" + filename);
Log.d("Destination is ", destination.toString());
SetToFolder.setEnabled(true);
}
}
source file
また、送信する必要がある関数を作成しましたdestination file
。これは、以前に作成したもので、これを新しいフォルダーにコピーします。
private void copy(File source, File destination) throws IOException {
FileChannel in = new FileInputStream(source).getChannel();
FileChannel out = new FileOutputStream(destination).getChannel();
try {
in.transferTo(0, in.size(), out);
} catch(Exception e){
Log.d("Exception", e.toString());
} finally {
if (in != null)
in.close();
if (out != null)
out.close();
}
}
また、このフォルダーが存在するかどうかを確認する関数を作成しました (存在destination file
しない場合はこのフォルダーを作成し、存在しない場合は何もしません。
private void DirectoryExist (File destination) {
if(!destination.isDirectory()) {
if(destination.mkdirs()){
Log.d("Carpeta creada","....");
}else{
Log.d("Carpeta no creada","....");
}
}
皆さんの協力に感謝します。皆さんと一緒に作成したこのコードを楽しんでいただければ幸いです :)