我已经完成了类似的答案,我想出的是我尝试编写我的第一个应用程序。 现在我遇到了错误。
我在一个活动中有一个ImageView,onclick调用一个方法(在相应的类中定义)
此方法调用相机,捕获图像并将其发送到另一个活动以供显示。 当我尝试在捕获它的同一活动中显示图像时,它只是工作正常 但是当我试图通过意图传递它时它会给我错误和错误。
这是我的代码 Activity1(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)
我尝试在捕获它的同一活动中显示图像,这就像一个魅力。但是当将图片网址传递给另一个活动时,它会给出错误。
任何人都可以指出我出了什么问题? 感谢
答案 0 :(得分:0)
像这样更改clickSelfie方法
public void clickSelfie() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, TAKE_PICTURE);
}
onactivityResult就像这样
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) {
Uri u = data.getData();
Intent selfiSrc = new Intent(this, SelfiEdit.class);
selfiSrc.putExtra("imgurl", u);
startActivity(selfiSrc);
}
}
接收活动就像这样创建
Bundle extras= getIntent().getExtras();
if(extras!=null)
{
path = (Uri) extras.get("imgurl");
}
ImageView img = (ImageView)findViewById(R.id.click_selfie);
img.setImageURI(path);
它就像我的设备中的魅力一样