英文:
Can't identify int in startActivityForResult?
问题
package com.example.feelhut;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class editprofile extends AppCompatActivity {
ImageButton proileimagebutton;
private static final int ImageRequest=1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
proileimagebutton = findViewById(R.id.edit_profile);
proileimagebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("imageeeeeee/");
There ImageRequest showing Error in startActivityForResult. Means I am not able to identify image request here.
startActivityForResult(intent.ImageRequest);
}
});
}
}
英文:
package com.example.feelhut;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class editprofile extends AppCompatActivity {
ImageButton proileimagebutton;
private static final int ImageRequest=1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
proileimagebutton= findViewById(R.id.edit_profile);
proileimagebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("imageeeeeee/");
There ImageRequest showing Error in startActivityForResult. Means I am not able to identify image request here.
startActivityForResult(intent.ImageRequest);
}
});
}
}
答案1
得分: 2
public class editprofile extends AppCompatActivity {
ImageButton proileimagebutton;
private static final int ImageRequest = 1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 没有内容视图?可以像这样添加:setContentView(R.layout.yourViewLayout);
proileimagebutton = findViewById(R.id.edit_profile);
proileimagebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, ImageRequest);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ImageRequest && resultCode == RESULT_OK) {
// 在这里设置你的 ImageView 的 URI:imageView.setImageURI(data.getData());
}
}
}
英文:
public class editprofile extends AppCompatActivity {
ImageButton proileimagebutton;
private static final int ImageRequest=1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//no content view? add like this setContentView(R.layout.yourViewLayout);
proileimagebutton= findViewById(R.id.edit_profile);
proileimagebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent,ImageRequest);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ImageRequest&& resultCode == RESULT_OK) {
// your ImageView.setUri(data.getData();
}
}
}
答案2
得分: 0
需要在"public class editprofile extends AppCompatActivity"之前声明int。
代码应该是:
private ImageButton proileimagebutton;
private static final int ImageRequest=1;
public class editprofile extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
proileimagebutton= findViewById(R.id.edit_profile);
proileimagebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("imageeeeeee/");
英文:
You need to declare int above "public class editprofile extends AppCompatActivity".
The code should be
private ImageButton proileimagebutton;
private static final int ImageRequest=1;
public class editprofile extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
proileimagebutton= findViewById(R.id.edit_profile);
proileimagebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("imageeeeeee/");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论