我已經經歷了類似的答案,我想到的是我試圖編碼我的第一個應用程序。 現在我遇到了錯誤。捕獲一個圖像,並將其傳遞給另一個活動在android
我有一個活性,其的onclick調用一個方法(在相應的類中定義)
此方法調用相機,捕捉圖像並將其發送到另一個活動用於顯示的ImageView的。 當我嘗試在捕獲它的同一活動中顯示圖像時,它只是正常工作 但是當我嘗試通過意圖傳遞它時,它會給我錯誤和錯誤。
這裏是我的代碼 活性1(fragment_selfin_review.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.ardortech.restofun.SelfinReview$PlaceholderFragment"
android:background="@color/black"
>
<ImageView
android:id="@+id/click_selfie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/blank_space"
android:layout_centerHorizontal="true"
android:contentDescription="@string/click_selfie"
android:gravity="center"
android:src="@drawable/ic_camera"
android:clickable="true"
android:onClick="clickSelfie" />
</RelativeLayout>
這裏是java類
package com.mysite.myapp;
import java.io.ByteArrayOutputStream;
import java.io.File;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class SelfinReview extends ActionBarActivity {
public static final int TAKE_PICTURE = 0;
private Uri mUri;
private Bitmap mPhoto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selfin_review);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
/** Code for onclick Camera Image Method
*
*/
public void clickSelfie(View v){
Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
File f = new File(Environment.getExternalStorageDirectory(), "photo.jpg");
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
mUri = Uri.fromFile(f);
startActivityForResult(i, TAKE_PICTURE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
if (resultCode == Activity.RESULT_OK) {
getContentResolver().notifyChange(mUri, null);
ContentResolver cr = getContentResolver();
try {
Intent selfiSrc = new Intent(this,SelfiEdit.class);
selfiSrc.putExtra("imgurl", mUri.toString());
startActivity(selfiSrc);
} catch (Exception e) {
Toast.makeText(this, e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_selfin_review,
container, false);
return rootView;
}
}
}
以及接收活動的代碼,我在這裏如何讓我的意圖
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selfi_edit);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
/**
* Receiving image data from the Previous activity
*/
ImageView previewThumbnail = (ImageView) findViewById(R.id.selfie_holder);
String rcvimgpath=getIntent().getStringExtra("imgurl");
previewThumbnail.setImageBitmap(BitmapFactory.decodeFile(rcvimgpath));
}
但是當我運行時,我得到這個錯誤就像
06-19 08:38:25.108: D/(1112): HostConnection::get() New Host Connection established
0xb8eae210, tid 1112
06-19 08:38:25.238: W/EGL_emulation(1112): eglSurfaceAttrib not implemented
06-19 08:38:25.288: D/OpenGLRenderer(1112): Enabling debug mode 0
06-19 08:38:28.438: W/EGL_emulation(1112): eglSurfaceAttrib not implemented
06-19 08:38:44.078: W/EGL_emulation(1112): eglSurfaceAttrib not implemented
06-19 08:38:44.668: E/BitmapFactory(1112): Unable to decode stream:
java.io.FileNotFoundException: /file:/storage/sdcard/photo.jpg: open failed: ENOENT (No
such file or directory)
06-19 08:38:44.758: D/AndroidRuntime(1112): Shutting down VM
06-19 08:38:44.758: W/dalvikvm(1112): threadid=1: thread exiting with uncaught
exception (group=0xb4a3fba8)
06-19 08:38:45.018: E/AndroidRuntime(1112): FATAL EXCEPTION: main
06-19 08:38:45.018: E/AndroidRuntime(1112): Process: com.mysite.myapp, PID: 1112
06-19 08:38:45.018: E/AndroidRuntime(1112): java.lang.RuntimeException: Unable to start
activity ComponentInfo{com.mysite.myapp/com.mysite.myapp.SelfiEdit}:
java.lang.NullPointerException
06-19 08:38:45.018: E/AndroidRuntime(1112):at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-19 08:38:45.018: E/AndroidRuntime(1112): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-19 08:38:45.018: E/AndroidRuntime(1112): at
android.app.ActivityThread.access$800(ActivityThread.java:135)
06-19 08:38:45.018: E/AndroidRuntime(1112): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-19 08:38:45.018: E/AndroidRuntime(1112): at
android.os.Handler.dispatchMessage(Handler.java:102)
06-19 08:38:45.018: E/AndroidRuntime(1112): at
android.os.Looper.loop(Looper.java:136)
06-19 08:38:45.018: E/AndroidRuntime(1112): at
android.app.ActivityThread.main(ActivityThread.java:5017)
06-19 08:38:45.018: E/AndroidRuntime(1112): at
java.lang.reflect.Method.invokeNative(Native Method)
06-19 08:38:45.018: E/AndroidRuntime(1112): at
java.lang.reflect.Method.invoke(Method.java:515)
06-19 08:38:45.018: E/AndroidRuntime(1112): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-19 08:38:45.018: E/AndroidRuntime(1112): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-19 08:38:45.018: E/AndroidRuntime(1112): at
dalvik.system.NativeStart.main(Native Method)
06-19 08:38:45.018: E/AndroidRuntime(1112): Caused by: java.lang.NullPointerException
06-19 08:38:45.018: E/AndroidRuntime(1112): at
com.mysite.myapp.SelfiEdit.onCreate(SelfiEdit.java:47)
06-19 08:38:45.018: E/AndroidRuntime(1112): at
android.app.Activity.performCreate(Activity.java:5231)
06-19 08:38:45.018: E/AndroidRuntime(1112): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-19 08:38:45.018: E/AndroidRuntime(1112): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
我試圖在它被捕獲的同一個活動中顯示圖像,並且像魅力一樣工作。但是在將圖片網址傳遞給其他活動時出現錯誤。
任何人都可以指出我是怎麼回事? 由於
謝謝@dumazy,但我想用clickSlefie()爲我定製的方法之一。我什至不知道這樣的事情存在;-)好吧,讓我試試你的建議,並恢復。 – echoashu
我跟在SOF上的一些其他答案線索,並得到了我的問題解決。想知道我是如何發佈我的解決方案?我可以回答我自己的問題嗎? – echoashu