当我想在活动中获取字符串时,会得到NULL



我试图将一个String从一个活动传递到另一个活动,但它返回NULL,该字符串具有我之前保存的图像的uri。

在活动一中,我有一个函数,它保存图像并将字符串URI放入类的变量中。接下来我输入代码。

活动一

public class PaintActivity extends AppCompatActivity implements PaintView {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paint);
tabLayout.setupWithViewPager(viewPager);
intent = new Intent(this, ResultsActivity.class);
readyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
paintImage.buildDrawingCache();
Bitmap bitmap = paintImage.getDrawingCache();
saveImage(bitmap);
presenter.getColorList();
startActivity(intent);
} catch(Exception e) {
e.getMessage();
}
}
});
}
private void saveImage(Bitmap finalBitmap) {
String root = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).toString();
File myDir = new File(root + "/Tersuave");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
// sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
//     Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this, new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
image_path = uri.toString();
intent.putExtra("image_path", image_path);
}
});
}

}

然后在onCreate方法中

paintImage.buildDrawingCache();
Bitmap bitmap = paintImage.getDrawingCache();
// this method is the one above 
saveImage(bitmap);
presenter.getColorList();
startActivity(intent);

活动二

Intent intent = getIntent();
// return null
String image = intent.getStringExtra("image_path");

一旦您在扫描结果后保留图像文件,您就可以执行类似的操作来开始下一个活动。

MediaScannerConnection.scanFile(this, new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
image_path = uri.toString();
Intent intent = new Intent(context,Activity2.class);
intent.putExtra("image_path", image_path);
startActivity(intent);
}
});

在Activity2中,你可以做这样的事情。

String imageFilePath = getIntent().getStringExtra("image_path");

查看您的实现,您正在触发活动ResultsActivity before,并将值image_path添加到被延迟的回调中。

MediaScannerConnection.scanFile(this, new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
image_path = uri.toString();
intent.putExtra("image_path", image_path); // this will be called after some time
}
});

presenter.getColorList();
startActivity(intent);

在OnScanCompletedListener中的intent.putExtra后面添加以上两行,并将其从onClickListener 中删除

在您的主要活动中,如果您想在意向中传递字符串,您应该这样做

FirstActivity

Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("Keyname", image_path);
startActivity(intent);

第二活动

on创建。。。。

if(savedInstanceState == null)
{
Bundle extras = getIntent().getExtras();
if (extras == null)
{
//Extra bundle is null
}else{
String image = extras.getString("Keyname");
}