[英]Capturing an image and passing it to another activity in android
I had gone through similar answers and what i came up with is i hav tried coding my first app. 我经历了类似的答案,我想到的是我尝试编写我的第一个应用程序。 Now i am running into errors.
现在我遇到了错误。
I have an ImageView in one activity, which onclick calls a method (defined in corresponding class) 我在一个活动中有一个ImageView,该onclick调用了一个方法(在相应的类中定义)
this method invokes camera , captures image and send it to another activity for display. 此方法调用相机,捕获图像并将其发送到另一个活动进行显示。 when i try to display image in same activity where it was captured, it just works fine but when i try to pass it via intent it gives me error and erros.
当我尝试在捕获图像的同一活动中显示图像时,它的效果很好,但是当我尝试通过意图传递图像时,会出现错误和错误。
here is my code Activity1 (fragment_selfin_review.xml) 这是我的代码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>
and here is the code for java class 这是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;
}
}
}
And in the receiving activity, here how i get my intent 在接收活动中,我如何得到我的意图
@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));
}
but when i am running, i am getting this error like 但是当我跑步时,我得到了这样的错误
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)
i tried displaying image in the same activity where it was captured, and that worked like a charm. 我尝试在与捕获图像相同的活动中显示图像,这就像一种魅力。 But its giving error when passing image url to another activity.
但是当将图像URL传递到另一个活动时,它给出了错误。
can anybody point me what is going wrong?? 有人可以指出我出了什么问题吗? Thanks
谢谢
Change the clickSelfie method like this 像这样更改clickSelfie方法
public void clickSelfie() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, TAKE_PICTURE);
}
onactivityResult like this 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);
}
} }
And the receiving activities oncreate like this 接收活动就这样创建
Bundle extras= getIntent().getExtras();
if(extras!=null)
{
path = (Uri) extras.get("imgurl");
}
ImageView img = (ImageView)findViewById(R.id.click_selfie);
img.setImageURI(path);
It works like a charm in my device 它在我的设备中就像魅力一样
this can help you more for wide number of devices 这可以为更多设备提供更多帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.