这是代码。 我尝试将图像路径从我的Main_Activity传递给这个新的Activity,但是图像没有显示在New_Activity中。
所以我的问题是:如何通过从Main_Activity传递路径来在New_Activity中显示图像?
有没有更简单的方法来做这样的事情?
如果可能,请详细说明。 也欢迎示例代码... 谢谢
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String filename = extras.getString("map1");
FileInputStream is = null;
BufferedInputStream bis = null;
try {
is = new FileInputStream(new File(filename));
bis = new BufferedInputStream(is);
Bitmap bitmap = BitmapFactory.decodeStream(bis);
myImage = (ImageView)findViewById(R.id.imageView2);
myImage.setImageBitmap(bitmap);
}
catch (Exception e) {
//Try to recover
}
finally {
try {
if (bis != null) {
bis.close();
}
if (is != null) {
is.close();
}
} catch (Exception e) {
}
}
myImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
这是logcat文件:
07-30 20:36:46.601: W/MapDbAdapter(387): 5
07-30 20:37:33.161: D/AndroidRuntime(387): Shutting down VM
07-30 20:37:33.161: W/dalvikvm(387): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-30 20:37:33.172: E/AndroidRuntime(387): FATAL EXCEPTION: main
07-30 20:37:33.172: E/AndroidRuntime(387): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.activity.map/com.example.activity.map.Activity2}: java.lang.NullPointerException
07-30 20:37:33.172: E/AndroidRuntime(387): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-30 20:37:33.172: E/AndroidRuntime(387): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-30 20:37:33.172: E/AndroidRuntime(387): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-30 20:37:33.172: E/AndroidRuntime(387): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-30 20:37:33.172: E/AndroidRuntime(387): at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 20:37:33.172: E/AndroidRuntime(387): at android.os.Looper.loop(Looper.java:123)
07-30 20:37:33.172: E/AndroidRuntime(387): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-30 20:37:33.172: E/AndroidRuntime(387): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 20:37:33.172: E/AndroidRuntime(387): at java.lang.reflect.Method.invoke(Method.java:521)
07-30 20:37:33.172: E/AndroidRuntime(387): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-30 20:37:33.172: E/AndroidRuntime(387): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-30 20:37:33.172: E/AndroidRuntime(387): at dalvik.system.NativeStart.main(Native Method)
07-30 20:37:33.172: E/AndroidRuntime(387): Caused by: java.lang.NullPointerException
07-30 20:37:33.172: E/AndroidRuntime(387): at com.example.activity.map.Activity2.onCreate(Activity2.java:48)
07-30 20:37:33.172: E/AndroidRuntime(387): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-30 20:37:33.172: E/AndroidRuntime(387): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-30 20:37:33.172: E/AndroidRuntime(387): ... 11 more
这是新的logcat文件:
07-30 21:12:25.921:W / MapDbAdapter(493):5 07-30 21:12:28.621:D / AndroidRuntime(493):关闭VM 07-30 21:12:28.621:W / dalvikvm(493):threadid = 1:线程退出未捕获异常(组= 0x4001d800) 07-30 21:12:28.631:E / AndroidRuntime(493):致命异常:主要 07-30 21:12:28.631:E / AndroidRuntime(493):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.activity.map/com.example.activity.map.Activity2}:java.lang 。空指针异常 07-30 21:12:28.631:E / AndroidRuntime(493):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 07-30 21:12:28.631:E / AndroidRuntime(493):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 07-30 21:12:28.631:E / AndroidRuntime(493):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125) 07-30 21:12:28.631:E / AndroidRuntime(493):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033) 07-30 21:12:28.631:E / AndroidRuntime(493):在android.os.Handler.dispatchMessage(Handler.java:99) 07-30 21:12:28.631:E / AndroidRuntime(493):在android.os.Looper.loop(Looper.java:123) 07-30 21:12:28.631:E / AndroidRuntime(493):在android.app.ActivityThread.main(ActivityThread.java:4627) 07-30 21:12:28.631:E / AndroidRuntime(493):at java.lang.reflect.Method.invokeNative(Native Method) 07-30 21:12:28.631:E / AndroidRuntime(493):at java.lang.reflect.Method.invoke(Method.java:521) 07-30 21:12:28.631:E / AndroidRuntime(493):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 07-30 21:12:28.631:E / AndroidRuntime(493):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 07-30 21:12:28.631:E / AndroidRuntime(493):at dalvik.system.NativeStart.main(Native Method) 07-30 21:12:28.631:E / AndroidRuntime(493):引起:java.lang.NullPointerException 07-30 21:12:28.631:E / AndroidRuntime(493):at com.example.activity.map.Activity2.onCreate(Activity2.java:29) 07-30 21:12:28.631:E / AndroidRuntime(493):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 07-30 21:12:28.631:E / AndroidRuntime(493):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 07-30 21:12:28.631:E / AndroidRuntime(493):... 11更多
错误在第29行:我知道了:这里也是我的java代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
String filename = getIntent().getStringExtra("map1");
Bitmap bitmap = BitmapFactory.decodeFile(filename);
if (bitmap != null) {
myImage = (ImageView)findViewById(R.id.imageView2);
myImage.setImageBitmap(bitmap);
}
myImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
// private Bitmap getBitmapFromAsset(String strName){
// AssetManager assetManager = getAssets();
// InputStream istr = null;
// try {
// istr = assetManager.open(strName);
// } catch (IOException e) {
// e.printStackTrace();
// }
// Bitmap bitmap = BitmapFactory.decodeStream(istr);
// return bitmap;
// }
}
答案 0 :(得分:0)
您正在做的事情是不正确的。使用此代码,让我们知道。
您可以使用AssetManager
使用其open()
方法获取InputStream,然后使用decodeStream()
BitmapFactory
方法获取位图。
private Bitmap getBitmapFromAsset(String strName)
{
AssetManager assetManager = getAssets();
InputStream istr = null;
try {
istr = assetManager.open(strName);
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(istr);
return bitmap;
}
答案 1 :(得分:0)
设置图像的路径并将图像保存到SD卡之后将图像路径(作为字符串获取的图像路径)发送到下一个活动,然后再次使用图像路径获取图像。
将图像保存到SD卡并为位图创建图像路径
private void saveImage() {
File rootsd = Environment.getExternalStorageDirectory();
File mediaStorageDir = new File(rootsd.getAbsolutePath()
+ "/YOUR_DIR/");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("log_check", "failed to create directory");
return;
}
}
String fileName= "your_file_name"
savedFileName = new File(mediaStorageDir.getAbsolutePath(), fileName
+ ".jpg");
try {
FileOutputStream fos = new FileOutputStream(savedFileName);
yourBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("FileNotFoundException", e + "");
} catch (IOException e) {
Log.e("IOException", e + "");
}
}
将您的“fileName”发送到下一个活动。
On Next Activty您可以使用您的文件名再次创建一个BitMap,如下面的代码。
String imagePath = getIntent().getExtras().getString("imagePath");
Bitmap yourBitmap = BitmapFactory.decodeFile(imagePath);
答案 2 :(得分:0)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String filename = getIntent().getStringExtra("map1");
Bitmap bitmap = BitmapFactory.decodeFile(filename);
if (bitmap != null) {
myImage = (ImageView)findViewById(R.id.imageView2);
myImage.setImageBitmap(bitmap);
}
答案 3 :(得分:0)
这就是问题的答案。它完美无瑕!但我通过图像而不是通过图像的路径,它更容易!
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
ImageView gallerypic = (ImageView) findViewById(R.id.imageView2);
/*Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.
*/
String backgroundpath = getIntent().getExtras().getString("maps");
if(getImageFromAssert(backgroundpath)!=null){
/*Sets a drawable as the content of this ImageView.*/
gallerypic.setImageDrawable(getImageFromAssert(backgroundpath));
}
}
private Drawable getImageFromAssert(String name){
try {
/*read the image file from the assets */
InputStream ims = getAssets().open(name);
/*create a drawable from the inputstream and return it */
return Drawable.createFromStream(ims, null);
}catch (Throwable e){
e.printStackTrace();
}
return null;
}