简体   繁体   中英

Get File Path from Intent in Android

My Application is a simple CSV Viewer and i want to read File path when someone opens a CSV file using my App.

I tried below code but it's giving me error as -----------------------------------------------------------------------------. java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = getIntent();
        String _file = intent.getData().toString();
        Log.d("INFO",_file);

    }
}

Sending Activity:

Intent intent = new Intent(context, MyActivity.class);
intent.putExtra("imagePath", pathToImage);
startActivity(intent);

Receiving Activity:

String path = getIntent().getStringExtra("imagePath");
 Intent intent = new Intent(context, SecondActivity.class);
 intent.putExtra("filePath", file.getAbsolutePath());
 startActivity(intent);

in MyActivity.class

 String path = getIntent().getStringExtra("filePath");
 File file=new File(path);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM