gpt4 book ai didi

java - 从android中的filedialog中获取图片并在imageview中显示

转载 作者:行者123 更新时间:2023-11-30 03:13:29 31 4
gpt4 key购买 nike

我想从文件选择器中拍照并在 ImageView 中显示它。但是当我运行我的应用程序时它会停止并且无法工作。问题在哪里?此代码是“MainActivity.java”的代码。我没有更改项目其他文件中的任何内容。我的代码基于 this link .我的代码是:

public class MainActivity extends Activity {

private static final int FILE_SELECT_CODE = 0;
private static final String TAG = null;
public TextView tv=(TextView) findViewById(R.id.textView1);
ImageView iv=(ImageView) findViewById(R.id.imageView1);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b=(Button) findViewById(R.id.button1);


b.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
showFileChooser();

}
});
}


private void showFileChooser() {

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);

try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case FILE_SELECT_CODE:
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
iv.setImageURI(uri);
Log.d(TAG, "File Uri: " + uri.toString());
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

日志是:

12-12 04:21:32.343: D/AndroidRuntime(1068): Shutting down VM
12-12 04:21:32.343: W/dalvikvm(1068): threadid=1: thread exiting with uncaught exception (group=0xb4a60b90)
12-12 04:21:32.433: E/AndroidRuntime(1068): FATAL EXCEPTION: main
12-12 04:21:32.433: E/AndroidRuntime(1068): Process: com.example.a55j, PID: 1068
12-12 04:21:32.433: E/AndroidRuntime(1068): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.a55j/com.example.a55j.MainActivity}: java.lang.NullPointerException
12-12 04:21:32.433: E/AndroidRuntime(1068): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102)
12-12 04:21:32.433: E/AndroidRuntime(1068): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
12-12 04:21:32.433: E/AndroidRuntime(1068): at android.app.ActivityThread.access$700(ActivityThread.java:135)
12-12 04:21:32.433: E/AndroidRuntime(1068): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
12-12 04:21:32.433: E/AndroidRuntime(1068): at android.os.Handler.dispatchMessage(Handler.java:102)
12-12 04:21:32.433: E/AndroidRuntime(1068): at android.os.Looper.loop(Looper.java:137)
12-12 04:21:32.433: E/AndroidRuntime(1068): at android.app.ActivityThread.main(ActivityThread.java:4998)
12-12 04:21:32.433: E/AndroidRuntime(1068): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 04:21:32.433: E/AndroidRuntime(1068): at java.lang.reflect.Method.invoke(Method.java:515)
12-12 04:21:32.433: E/AndroidRuntime(1068): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-12 04:21:32.433: E/AndroidRuntime(1068): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-12 04:21:32.433: E/AndroidRuntime(1068): at dalvik.system.NativeStart.main(Native Method)
12-12 04:21:32.433: E/AndroidRuntime(1068): Caused by: java.lang.NullPointerException
12-12 04:21:32.433: E/AndroidRuntime(1068): at android.app.Activity.findViewById(Activity.java:1883)
12-12 04:21:32.433: E/AndroidRuntime(1068): at com.example.a55j.MainActivity.<init>(MainActivity.java:23)
12-12 04:21:32.433: E/AndroidRuntime(1068): at java.lang.Class.newInstanceImpl(Native Method)
12-12 04:21:32.433: E/AndroidRuntime(1068): at java.lang.Class.newInstance(Class.java:1208)
12-12 04:21:32.433: E/AndroidRuntime(1068): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
12-12 04:21:32.433: E/AndroidRuntime(1068): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
12-12 04:21:32.433: E/AndroidRuntime(1068): ... 11 more
12-12 04:21:34.823: I/Process(1068): Sending signal. PID: 1068 SIG: 9

activiyt_main 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=".MainActivity" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginLeft="16dp"
android:layout_marginTop="35dp"
android:layout_toRightOf="@+id/textView1"
android:text="@string/open" />

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginTop="81dp"
android:layout_toLeftOf="@+id/button1"
android:src="@drawable/ic_launcher" />

</RelativeLayout>

最佳答案

试试这个-

 public class MainActivity extends Activity {

private static final int FILE_SELECT_CODE = 0;
private static final String TAG = null;
public TextView tv;
public ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView) findViewById(R.id.textView1);
iv=(ImageView) findViewById(R.id.imageView1);
Button b=(Button) findViewById(R.id.button1);


b.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
showFileChooser();

}
});
}


private void showFileChooser() {

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);

try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case FILE_SELECT_CODE:
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
iv.setImageURI(uri);
Log.d(TAG, "File Uri: " + uri.toString());
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

关于java - 从android中的filedialog中获取图片并在imageview中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20539201/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com