我有两个活动,我在一个活动中使用相机,而另一个活动只有一个imageview来显示我使用相机拍摄的照片。我能够在拍摄照片的第一个活动中成功查看图片,并且我想将图像(或)图像路径发送到下一个活动以显示图像并添加一些效果,但我无法做到。
代码:(更新)
发送活动:
Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
bitmapOptions);
view_image.setImageBitmap(bitmap);
String path = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "Pictures" + File.separator + "SanPics2";
System.out.println(path);
f.delete();
OutputStream outFile = null;
int count = 0;
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
int defaultValue = getPreferences(MODE_PRIVATE).getInt("count_key",count);
++defaultValue;
getPreferences(MODE_PRIVATE).edit().putInt("count_key", defaultValue).commit();
count = getPreferences(MODE_PRIVATE).getInt("count_key", count);
System.out.println("The value if count is " + count);
image_name_2 = "Image " + count;
File file = new File(path,image_name_2 + ".jpg");
try {
outFile = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
Intent image_intent = new Intent(Snap_ImageView.this,FullScreen.class);
Log.i("SnapImageView",image_name_2);
image_intent.putExtra("image_path" ,image_name_2 );
Log.i("SnapImageView","Extra has been passed");
System.out.println("The image name is " + image_name_2);
startActivity(image_intent);
outFile.flush();
outFile.close();
}
接收活动:
String path = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "Pictures" + File.separator + "SanPics2";
System.out.println("Reached FullScreen");
location = getIntent().getStringExtra("image_path");
System.out.println("Got the extra" + location);
File image_file = new File(path + File.separator + location);
String fil_value = image_file.toString();
System.out.println("The full image name is " + fil_value);
{
OutputStream outfile = null;
outfile = new FileOutputStream(image_file);
Log.i("Inside try block","At OutputStream");
bmp = BitmapFactory.decodeFile(location);
Log.i("Inside try block","Decoding is over");
bmp.compress(Bitmap.CompressFormat.JPEG, 85,outfile);
Log.i("Inside try block","File is compressed");
iv.setImageBitmap(bmp);
Logcat:
12-31 12:24:38.662: I/dalvikvm-heap(9245): Grow heap (frag case) to 25.995MB for 24023056-byte allocation
12-31 12:24:39.203: D/dalvikvm(9245): GC_FOR_ALLOC freed 20K, 3% free 26515K/27299K, paused 23ms
12-31 12:24:39.253: I/System.out(9245): /mnt/sdcard/Pictures/SanPics2
12-31 12:24:39.403: I/System.out(9245): The value if count is 24
12-31 12:24:40.814: I/SnapImageView(9245): Image 24
12-31 12:24:40.814: I/SnapImageView(9245): Extra has been passed
12-31 12:24:40.814: I/System.out(9245): The image name is Image 24
12-31 12:24:41.145: D/memalloc(9245): /dev/pmem: Mapped buffer base:0x51e83000 size:28549120 offset:26460160 fd:58
12-31 12:24:41.435: I/System.out(9245): Reached FullScreen
12-31 12:24:41.435: I/System.out(9245): Got the extraImage 24
12-31 12:24:41.435: I/System.out(9245): The full image name is /mnt/sdcard/Pictures/SanPics2/Image 24
12-31 12:24:41.435: I/Inside try block(9245): At OutputStream
12-31 12:24:41.435: I/Inside try block(9245): Decoding is over
12-31 12:24:41.465: D/AndroidRuntime(9245): Shutting down VM
12-31 12:24:41.465: W/dalvikvm(9245): threadid=1: thread exiting with uncaught exception (group=0x40ab1228)
12-31 12:24:41.515: E/AndroidRuntime(9245): FATAL EXCEPTION: main
12-31 12:24:41.515: E/AndroidRuntime(9245): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.facebook_integration/com.example.facebook_integration.FullScreen}: java.lang.NullPointerException
12-31 12:24:41.515: E/AndroidRuntime(9245): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
12-31 12:24:41.515: E/AndroidRuntime(9245): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
12-31 12:24:41.515: E/AndroidRuntime(9245): at android.app.ActivityThread.access$600(ActivityThread.java:139)
12-31 12:24:41.515: E/AndroidRuntime(9245): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
12-31 12:24:41.515: E/AndroidRuntime(9245): at android.os.Handler.dispatchMessage(Handler.java:99)
12-31 12:24:41.515: E/AndroidRuntime(9245): at android.os.Looper.loop(Looper.java:154)
12-31 12:24:41.515: E/AndroidRuntime(9245): at android.app.ActivityThread.main(ActivityThread.java:4945)
12-31 12:24:41.515: E/AndroidRuntime(9245): at java.lang.reflect.Method.invokeNative(Native Method)
12-31 12:24:41.515: E/AndroidRuntime(9245): at java.lang.reflect.Method.invoke(Method.java:511)
12-31 12:24:41.515: E/AndroidRuntime(9245): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-31 12:24:41.515: E/AndroidRuntime(9245): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-31 12:24:41.515: E/AndroidRuntime(9245): at dalvik.system.NativeStart.main(Native Method)
12-31 12:24:41.515: E/AndroidRuntime(9245): Caused by: java.lang.NullPointerException
12-31 12:24:41.515: E/AndroidRuntime(9245): at com.example.facebook_integration.FullScreen.onCreate(FullScreen.java:64)
12-31 12:24:41.515: E/AndroidRuntime(9245): at android.app.Activity.performCreate(Activity.java:4531)
12-31 12:24:41.515: E/AndroidRuntime(9245): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
12-31 12:24:41.515: E/AndroidRuntime(9245): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
12-31 12:24:41.515: E/AndroidRuntime(9245): ... 11 more
Logcat显示图像未设置,但已完成解码,压缩是错误的位置。图像以指定的日期名称存储,并存储在指定的确切位置。
如果我对压缩线进行评论,那么我将在ImageView中获得纯白色背景。
如果有人发现任何奇怪的代码,请告诉我。
任何帮助将不胜感激。任何更新,随时可以询问。
答案 0 :(得分:2)
您可以将位图设置为Imageview,如下所示:
File imgFile = new File(
"yourpath/" + file_name(Specific file name with extension));
//System.out.println("Image Exists:::" + imgFile.getAbsolutePath().toString());
if (imgFile.exists()) {
// System.gc();
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile
.getAbsolutePath());
//System.out.println("Image Exists:::");
imageview.setImageBitmap(myBitmap);
}
您应该检查第一个图像是否存在于指定位置,如果存在,则解码图像并设置为imageview,然后不显示某些错误消息。
答案 1 :(得分:1)
图片的扩展名在哪里。你还需要指定扩展名。
答案 2 :(得分:1)
我知道这将是一个很大的阅读,但这肯定会解决你的问题。 Bitmap
答案 3 :(得分:1)
仅当图像路径错误时才会出现此问题。您应该检查图像路径。
答案 4 :(得分:0)
我已经部分找到了从路径中在图像视图中设置图像的答案。但我还没有在位图中实现压缩。我在下面的代码中删除了位图压缩行。
代码:
String path = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "Pictures" + File.separator + "SanPics2";
System.out.println("Reached FullScreen");
location = getIntent().getStringExtra("image_path");
System.out.println("Got the extra" + location);
File image_file = new File(path + File.separator + location);
String fil_value = image_file.toString() + ".jpg"; // Add the image extension along with the image name.
System.out.println("The full image name is " + fil_value);
Log.i("Inside try block","At OutputStream");
bmp = BitmapFactory.decodeFile(fil_value); // I had passed the location variable which has the image name but not with its extension and then I added the fil_value which has the image name along with the extension
String bit = bmp.toString();
Log.i("Bitmap is",bit);
Log.i("Inside try block","Decoding is over");
Log.i("Inside try block","File is compressed");
iv.setImageBitmap(bmp);
不要忘记添加文件名的扩展名及其准确路径。仔细检查图像是否存在于指定位置。我感谢所有回答这些问题的用户帮助我解决问题。